Postman extension get a response, but my jquery request not [duplicate]

萝らか妹 提交于 2019-11-29 08:53:59

问题


This question already has an answer here:

  • Why does my JavaScript code get a “No 'Access-Control-Allow-Origin' header is present on the requested resource” error when Postman does not? 44 answers

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 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.

Why does this happen?

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;
    });
}

The Postman extension is not on the same domain either, why does it get a response?


回答1:


Just to help future fellows seeking for this specific question: Why POSTMAN works and my jQuery don't!

The answer is quite simple, actually: Chrome Extensions are allowed to do so!

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



来源:https://stackoverflow.com/questions/25291840/postman-extension-get-a-response-but-my-jquery-request-not

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!