Home >Web Front-end >JS Tutorial >Why is my jQuery AJAX JSONP request failing to return data from a cross-domain source?
When attempting to parse JSON array data using jQuery AJAX with the provided code, the user is not receiving any output.
Cross-domain AJAX calls require that the web service supports method injection for JSONP to function. The client code seems valid, but the issue may lie in the domain of the web application and the web service.
When using jQuery's dataType: 'jsonp', a parameter ?callback={some_random_dynamically_generated_method} is added to the query URL. This method acts as a proxy in the window object, similar to:
window.some_random_dynamically_generated_method = function(actualJsonpData) { successCallback(actualJsonData); // Calls the success function passed to $.ajax }
To address the problem, the server-side code must be modified to wrap the JSON data in a function name that matches the one passed with the query string. For example, if the query string includes ?callback=my_callback_method, the server's response should be:
my_callback_method({your json serialized data})
The above is the detailed content of Why is my jQuery AJAX JSONP request failing to return data from a cross-domain source?. For more information, please follow other related articles on the PHP Chinese website!