To perform a cross-domain AJAX request with JSONP in jQuery, you can use the $.ajax()
function with the dataType: 'jsonp'
option. Here is an example:
$.ajax({
url: 'https://api.example.com/data',
dataType: 'jsonp',
success: function(response) {
console.log(response);
},
error: function(xhr, status, error) {
console.log(error);
}
});
In the above example, the url
parameter specifies the URL of the API endpoint you want to request data from. The dataType
parameter is set to 'jsonp'
to indicate that you want to use JSONP.
The success
callback function is called when the request is successful, and the response data is passed as an argument. You can use this callback function to handle the response data. In the example, the response is logged to the console.
The error
callback function is called when there is an error in the request, and the details of the error (such as the status code and error message) are passed as arguments. In the example, the error message is logged to the console.