JSON: How do I make cross-domain JSON call

前端 未结 8 914
情歌与酒
情歌与酒 2020-11-30 06:54

I try to run the following jquery code in local Network.

 $.ajax({
     type: \"GET\",
     url: \"http://SomeSite/MyUrl/\",
     cache: false,
     data: {         


        
8条回答
  •  半阙折子戏
    2020-11-30 07:35

    If you have access to the server that you want to load resources/data from you might modify the request headers of the servers response to include

    "Access-Control-Allow-Origin", "*"

    The Same Origin Policy enforced by the browsers - as far as I know in varying degrees of strictness depending on the browser - is (partially?) based on the values of the response headers.

    I had the same issue when trying to load json from a webservice. All the JS hacks I found to get around that didn't really work and I was wondering why I even have to do this, if I want to load data from a server that I myself control (and trust). Then I learned that the response headers of the server play a vital role in this whole issue. When I added the above mentioned header to the http response of my webservice, the problem was solved.

提交回复
热议问题