Postman extension get a response, but my jquery request not
本问题已经有最佳答案,请猛点这里访问。
我使用Postman扩展创建一个GET请求并获得响应,但如果我使用jQuery发出相同的请求,我会收到一个典型的错误:
XMLHttpRequest cannot load
http://www.rfen.es/publicacion/ranking/resultsBySwimmer.asp?l=020039535&t=&p=0&e=50L-I.
No 'Access-Control-Allow-Origin' header is present on the requested
resource.
为什么会这样?
我的javascript代码很简单:
1 2 3 4 5 6 7 8 9 10 11 | function getTiempo (dni, piscina, prueba) { $.ajax({ async: false, type:"GET", url:"http://www.rfen.es/publicacion/ranking/resultsBySwimmer.asp?l="+dni+"&t=&p="+piscina+"&e="+prueba }) .done(function (data) { console.log(data); return data; }); } |
Postman扩展也不在同一个域上,为什么会得到响应?
只是为了帮助未来的研究人员寻找这个特定的问题:为什么POSTMAN有效,而我的jQuery却没有!
答案很简单,实际上:允许Chrome扩展程序这样做!
Regular web pages can use the XMLHttpRequest object to send and receive data from remote servers, but they're limited by the same origin policy. Extensions aren't so limited. An extension can talk to remote servers outside of its origin, as long as it first requests cross-origin permissions.
https://developer.chrome.com/extensions/xhr