我是JSONP的新手,我正在尝试访问我的网络服务器上托管的跨域文件。
更新:我找到了解决问题的另一个解决方案:我使用CORS进行http://www.html5rocks.com/en/tutorials/cors/的JQUERY
这是我的js文件:
var url = "myip:port/pgif.jsonp?callback=?"
var url2 = "http://puppygifs.tumblr.com/api/read/json?callback=?";
$.getJSON(url2, {
tags: "jquery",
tagmode: "any",
format: "json"
}, function (data) {
console.log("d= ", data);
});
}
);
当我尝试使用url2时,它运行良好但是当我尝试使用url访问我的网络服务器上的文件时却没有。
以下是我在Chrome开发工具的网络标签中看到的,其中url2可以正常工作:
Request URL:http://puppygifs.tumblr.com/api/read/json?callback=jQuery19107969533477444202_1386114768597&tags=jquery&tagmode=any&format=json&_=1386114768598
Request Method:GET
Status Code:200 OK
Request Headersview source
Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en,fr-FR;q=0.8,fr;q=0.6
Cookie:__utma=189990958.626465526.1385955515.1385955515.1385955515.1; __utmc=189990958; __utmz=189990958.1385955515.1.1.utmcsr=sarajchipps.com|utmccn=(referral)|utmcmd=referral|utmcct=/2011/01/using-jquery-ajax-calls-in-a-chrome-plugin.html
Host:puppygifs.tumblr.com
Proxy-Authorization:Negotiate TlRMTVNTUAADAAAAGAAYAIQAAABiAWIBnAAAAAwADABYAAAAEAAQAGQAAAAQABAAdAAAABAAEAD+AQAAFYKI4gYBsR0AAAAPwICK4LzPlRZWoe1WgJ+erkYATwBYAFQARQBMAGIAYQBzAHQAaQBkAGUAZgBIAFEAMAAyADcANAAzADUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA+t3F+qb2rUYkHj2KQg3XtAEBAAAAAAAAanrqyYLwzgG/iL4+KqlreQAAAAACAAwARgBPAFgAVABFAEwAAQAQAFMAWQBEAFQATQBHADAAMQAEACIAZQBuAHQALgBmAG8AeAB0AGUAbAAuAGMAbwBtAC4AYQB1AAMANABTAFkARABUAE0ARwAwADEALgBlAG4AdAAuAGYAbwB4AHQAZQBsAC4AYwBvAG0ALgBhAHUABQAiAGUAbgB0AC4AZgBvAHgAdABlAGwALgBjAG8AbQAuAGEAdQAHAAgAanrqyYLwzgEGAAQAAgAAAAgAMAAwAAAAAAAAAAAAAAAAIAAA+jYk8RidWxnk5XJtyRLc7j6jPZX/e7BWswB5GZeS2JYKABAAAAAAAAAAAAAAAAAAAAAAAAkAIgBIAFQAVABQAC8AMQAwAC4AMgAwAC4AMQA2AC4AMQAwADAAAAAAAAAAAAAAAAAA2S3kxHzp0FstuiERqIRwqQ==
Proxy-Connection:keep-alive
Referer:http://ip:port/MyApp/index.html
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36
Query String Parametersview sourceview URL encoded
callback:jQuery19107969533477444202_1386114768597
tags:jquery
tagmode:any
format:json
_:1386114768598
Response Headersview source
Cache-Control:max-age=900
Connection:Keep-Alive
Content-Type:text/javascript
Date:Tue, 03 Dec 2013 23:52:57 GMT
Last-Modified:Sun, 03 Feb 2013 12:02:52 GMT
P3P:CP="ALL ADM DEV PSAi COM OUR OTRo STP IND ONL"
Proxy-Connection:Keep-Alive
Transfer-Encoding:chunked
Vary:X-UA-Device
Via:1.1 SYDTMG01
X-Cache-Metadata:
X-Robots-Tag:noindex
X-UA-Device:desktop
以下是我在Chrome开发工具的网络标签中看到的,其中网址无效:
Request URL:http://ip:port/pgif.jsonp?callback=jQuery1910627754604909569_1386116200762&tags=jquery&tagmode=any&format=json&_=1386116200763
Request Method:GET
Status Code:200 OK
Request Headersview source
Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en,fr-FR;q=0.8,fr;q=0.6
Connection:keep-alive
Host:ip:port
Referer:http://ip:port/MyApp/index.html
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36
Query String Parametersview sourceview URL encoded
callback:jQuery1910627754604909569_1386116200762
tags:jquery
tagmode:any
format:json
_:1386116200763
Response Headersview source
Accept-Ranges:bytes
Access-Control-Allow-Origin:*
Content-Length:6616
Content-Type:application/javascript
Date:Thu, 01 Jan 1970 02:02:33 GMT
ETag:"68647623"
Last-Modified:Thu, 01 Jan 1970 01:23:16 GMT
Server:lighttpd/1.4.30
正如您所看到的,在上一篇文章中,我在lighttpd网络服务器的配置中添加了Access-Control-Allow-Origin:*,这并没有改善这种情况。
所以我真的不明白,我收到了来自网络服务器的答案,但我的成功函数从未被调用过。
这是我试图在我的网络服务器上访问的JSONP文件:
var tumblr_api_read = {"tumblelog":{"title":"puppy gifs"},"posts-start":0,"posts-total":"18"}
答案 0 :(得分:0)
所以,我没有设法使JSONP与我的Web服务器一起工作,但我发现了一种不同的方法来解决我的问题: 我跟随了JQuery"" CORS。来自html5rocks.com/en/tutorials/cors 我现在能够在我的网络服务器上阅读我的文件。
似乎我想编辑我的文件,我需要配置我的Web服务器才能处理预检请求。如x3dom.readthedocs.org/en/latest/notes/cors.html
中所述