ajax - Using jQuery/Jsonp to send cross-domain request and parse result? -
i'm trying , parse data remote cross-domain site jquery.to avoid same-origin-policy , cross-domin issue, use jsonp.
<html> <head> <title>ajax sample</title> <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.min.js"></script> </style> <script> $(document).ready(function(){ //obviously service wont give json format response var url='http://stackoverflow.com/search?q=cross+domain'; $.ajax({ url:url, datatype: 'jsonp', success:function(data){ console.log(data); }, error:function(){ alert("error"); }, }); }); </script> <body> </body> </html>
but got error
:
resource interpreted script transferred mime type text/html: "http://stackoverflow.com/search?q=cross+domain&callback=jquery1708665772899985313_1374154944485&_=1374154944492"
and
uncaught syntaxerror: unexpected token <
so how make in right way?
the first warning normal if remote end not set correct content-type
header jsonp output. it's warning, causes no harm.
if second error trying access stackoverflow website error because you're trying throw html code @ js interpreter.
note jsonp request must receive json in form of valid js script. won't work other type of input.
for security reasons cannot access other types of resource other origins unless resources send appropriate access-control-allow-origin
http headers.
Comments
Post a Comment