The server won't accept any parameters in a request URL, so I need to remove all the extra parameters in the URL and of course I can't control the server.
jQuery:
$.ajax({
type: 'GET',
url: 'http://cross-domain.com/the_jsonp_file,
jsonpCallback: 'jsonCallback',
contentType: 'application/json',
cache: 'true',
dataType: 'jsonp',
success: function(json) {
console.log(json);
},
});
The JSONP file:
jsonCallback({"test": "hello"});
When I send that Ajax request, the URL looks like this:
http://cross-domain.com/the_jsonp_file?callback=jsonCallback
But I need this (without parameters):
http://cross-domain.com/the_jsonp_file
EDIT:
Here is my whole situation:
function MyClass(imgs) {
// imgs is array of URLs
this.imgs = imgs;
this.submit = function() {
// button click event triggers this method
this._show();
};
this._show = function() {
var _this = this;
for (var i = 0; i < _this.imgs.length; i++) {
(function($, j) {
$.ajax({
type: 'GET',
url: _this.imgs[j],
jsonp : false,
jsonpCallback: 'jsonCallback',
cache: 'true',
dataType:'jsonp',
success: function(json) {
console.log(_this.imgs[j]);
},
});
})(jQuery, i);
};
};
};
And I got this error message:
Uncaught TypeError: Property 'jsonCallback' of object [object Window] is not a function
Weird thing is few requests are successfully calling jsonCallback.
Callback means that it will call the JavaScript function whose name is specified in the Callback parameter once the response is ready. In this article I will explain JSONP request with Callback example using JavaScript i.e. how to make a Cross Domain JSONP request with Callback using plain JavaScript without using jQuery.
This is a simple process to implement jsonp in jquery. 1) In this example we are using JSONP to request a cross-origin domain in JQuery to get the data. For instance, we are using a callback URL from git hub to make this example work. This is a sample example for beginners to understand the jsonp concept in JQuery.
As soon as the Script Tag is attached to the page, it makes call to the JSON Web Service along with the Callback function’s name DisplayIP as Callback parameter. The JSON Web Service returns the response in the specified Callback JavaScript function DisplayIP, which accepts the JSON response and displays it in HTML Span element.
2) datatype: This parameter is used to set the type. Here we are setting ‘application/JSON’ as the type of the file. This is also referred to as the content of the file. How JSONP works in jQuery? As of now, we know that jsonp is used to handle the JSON over the network.
Check the jQuery docs - they say to say jsonp: false and jsonpCallback : 'callbackFunction' in the ajax args....like:
$.ajax({
url: 'http://cross-domain.com/the_jsonp_file',
jsonp : false,
jsonpCallback: 'jsonCallback',
// contentType: 'application/json', -- you can't set content type for a <script> tag, this option does nothing for jsonp | KevinB
cache: 'true',
dataType : 'jsonp'
});
http://api.jquery.com/jQuery.ajax/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With