问题描述
我使用 Postman 扩展发出 GET 请求并获得响应,但是如果我使用 jQuery 发出相同的请求,我会收到一个典型的错误:
I make a GET request using Postman extension and obtain a response, but if I make the same request using jQuery I receive a typical error:
XMLHttpRequest 无法加载http:///www.rfen.es/publicacion/ranking/resultsBySwimmer.asp?l=020039535&t=&p=0&e=50L-I.请求中不存在Access-Control-Allow-Origin"标头资源.
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 代码很简单:
My javascript code is simple:
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 扩展也不在同一个域中,为什么会得到响应?
The Postman extension is not on the same domain either, why does it get a response?
推荐答案
只是为了帮助未来寻求这个特定问题的人:为什么 POSTMAN 有效而我的 jQuery 无效!
Just to help future fellows seeking for this specific question: Why POSTMAN works and my jQuery don't!
实际上,答案很简单:Chrome 扩展程序可以这样做!
The answer is quite simple, actually: Chrome Extensions are allowed to do so!
常规网页可以使用 XMLHttpRequest 对象从远程服务器发送和接收数据,但它们受到同源策略的限制.扩展并没有那么有限.只要首先请求跨域权限,扩展程序就可以与其源之外的远程服务器通信.
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
这篇关于邮递员扩展得到响应,但我的 jquery 请求没有的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!