jquery $.getJSON() cross-domain request

巴扎黑
Release: 2017-07-03 09:44:33
Original
940 people have browsed it

I never understood what was going on before, but now I have no choice, so I read the documentation carefully, and finally the test was successful. Note

1, and other requests under the same domain name can be The same
js:

The code is as follows:

var url="http://localhost:2589/a.ashx";
$(function() {
$.getJSON(url,function(data){
alert (data.Name);
})
});


Server returnsString:
{"Name":"loogn","Age":23}
2, under different domain names
js:

Code As follows:

var url="http://localhost:2589/a.ashx?callback=?";
$(function(){
$.getJSON(url,function(data ){
alert (data.Name);
})
});


The server returns a string:
jQuery1706543070425920333_1324445763158({"Name":"loogn","Age":23})
The returned string is afunctioncalled "jQuery1706543070425920333_1324445763158", the parameter is {"Name":" loogn","Age":23}.
In fact, this very long function name is the function of callback=? in the request path. I think it should be like this: The $.getJSON method generates aname that references the callback method, replace it? . The above request will becomehttp://localhost:2589/a.ashx?callback=jQuery1706543070425920333_1324445763158&_=1324445763194. The server needs to process it when returning json, such as:

The code is as follows:

stringcb = context.Request["callback"];context.Response.Write(cb + "(" + json + ")");

The parameter name callback can also be replaced by jsoncallback. I think it is because of fear of conflict. jsoncallback should be detected first, and callback should not be detected again (not tested!!)
? It can also be a specific function name, so that
callback functioncannot be anonymous. Use? Generation is just a convenience provided by jQuery for our general operations.

The above is the detailed content of jquery $.getJSON() cross-domain request. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!