Today's problem is title, simply said that is the cross-domain of Ajax, the first problem is that, in the container (TOMCAT6) new test html--test.html, call in the container in the project XXX method, all the process code is not a problem, The JSON format string is returned in the method and is returned in JSON like this:
<result name= "Success" type= "JSON" >
<param name= "Root" >json</param>
</result>
Page code:
$.get ("http://localhost:8080/xxx/queryInfo.action?id=111",
function (data) {
Alert (Data.name)
}, "JSON")
Using FF's Firebug debugging to find that the test.html request has passed, the server state 200, the delay of 40ms, but the entire request is red to represent the error, then the test.html in the request URL paste into the address bar, found everything normal, after the tangled ....
Using Chrome's console Debug discovery:
XMLHttpRequest cannot load http://localhost:8080/xxx/queryInfo.action?id=111. No ' Access-control-allow-origin ' header is present on the requested resource. Origin ' null ' is therefore not allowed access.
Degree Niang later found is a cross-domain problem,
After referencing http://www.cnblogs.com/chopper/archive/2012/03/24/2403945.html, change the code to
$.get ("http://localhost:8080/xxx/queryInfo.action?id=111&callback=?",
function (data) {
Alert (Data.name)
}, "Jsonp")
After debug Discovery report uncaught syntaxerror:unexpected token: This error, open error code found the returned JSON format error, because in the request headers Riga so 2 paragraph
callback:jquery110209291235436685383_1437019561773
_:1437019561774
Then can not parse the second ":", so the error unexpected token:
Continue to Niang later found that for the JSONP have special format needs to change,
After referencing http://kb.cnblogs.com/page/139725/, it is necessary to change the return value of the package, but there is no ready-made JSON to JSONP tool, technology, encapsulation class (such as String to JSON and other tools), need to write manually, Decisively give up to continue debugging ...
Continue the Niang inadvertently found this post
http://blog.csdn.net/z69183787/article/details/45896577 The return value in the Struts.xml file is added to a
<param name= "Callbackparameter" >callback</param>
Perfect Solution ~
At this point the problem solved for 2 hours, hereby recorded
Ps:jquery in the JSONP package, the Get type request support is good, it is recommended to use the Get method request, it is best not to use the post
PS: There is also a violent cross-domain approach that seems to support only Chrome and Android systems,
Refer to Http://camnpr.com/archives/chrome-args-disable-web-security.html, by using --disable-web-security This parameter forcibly aborts cross-domain security checks, the final result is the same, but security, the user is well aware of the answer to the problem http://www.zhihu.com/question/27817605
Using AJAX to access issues based on SSH2 methods in independent HTML