Same origin policy

前端 未结 4 655
悲哀的现实
悲哀的现实 2020-12-06 13:59

Maybe some of you can help me get a better understanding of the javascript same origin policy.

The same origin policy is defined as following (http://en.wikipedia.or

4条回答
  •  天命终不由人
    2020-12-06 14:53

    Here's what you need to do: JSONP.

    Because of said policy you can't make an AJAX request to yahoo, but there are workarounds. Namely, the script tag, which can make a request to anywhere.

    For example, say you want to do the request to yahoo when a user clicks the "GO" button. You need to add an event handler to catch the user's click event then add a new script tag to the head section of the DOM. The URL of the script tag is important, it must have a callback param in it, e.g.:

    http://helloasdf.cloudfoundry.com/get.tokens?callback=xss

    Note callback can be any arbitrary function name. The response will be:

    xss(["asdf"])
    

    meaning that the xss function in your code will be passed ["asdf"].

    Or w/ yahoo's API;

    http://d.yimg.com/autoc.finance.yahoo.com/autoc?query=GS&callback=YAHOO.Finance.SymbolSuggest.ssCallback

    notice the callback=YAHOO.Finance.SymbolSuggest.ssCallback it will call that function when the request returns:

    YAHOO.Finance.SymbolSuggest.ssCallback({"ResultSet":{"Query":"gs","Result":[{"symbol":"GS","name": "The Goldman Sachs Group, Inc.","exch": "NYQ","type": "S","exchDisp":"NYSE","typeDisp":"Equity"},{"symbol":"^GSPC","name": "S&P 500 INDEX,RTH","exch": "SNP","type": "I","typeDisp":"Index"},{"symbol":"GSS","name": "Golden Star Resources, Ltd.","exch": "ASE","type": "S","exchDisp":"AMEX","typeDisp":"Equity"},{"symbol":"^GSPTSE","name": "S&P/TSX Composite index (Interi","exch": "TOR","type": "I","exchDisp":"Toronto","typeDisp":"Index"},{"symbol":"GSK","name": "GlaxoSmithKline plc","exch": "NYQ","type": "S","exchDisp":"NYSE","typeDisp":"Equity"},{"symbol":"GSX","name": "Gasco Energy Inc.","exch": "ASE","type": "S","exchDisp":"AMEX","typeDisp":"Equity"},{"symbol":"OIL","name": "iPath S&P GSCI Crude Oil TR Index ETN","exch": "PCX","type": "E","typeDisp":"ETF"},{"symbol":"GSIC","name": "GSI Commerce Inc.","exch": "NMS","type": "S","exchDisp":"NASDAQ","typeDisp":"Equity"},{"symbol":"GST","name": "Gastar Exploration, Ltd.","exch": "ASE","type": "S","exchDisp":"AMEX","typeDisp":"Equity"},{"symbol":"GSI","name": "General Steel Holdings, Inc.","exch": "NYQ","type": "S","exchDisp":"NYSE","typeDisp":"Equity"}]}})
    

    Here is an example of the js you need to dynamically add the script tag:

     var headLoc = document.getElementsByTagName("head").item(0);
     var scriptObj = document.createElement("script");
     var token="localstring"
     var url="http://helloasdf.cloudfoundry.com/get.tokens?callback=xssCallback";
      // Add script object attributes
      scriptObj.setAttribute("type", "text/javascript");
      scriptObj.setAttribute("charset", "utf-8");
      scriptObj.setAttribute("src", url);
      scriptObj.setAttribute("id", 'asf12');
    
      headLoc.appendChild(scriptObj);
    

    I've documented the process more here: http://eggie5.com/22-circumvent-same-origin-policy

提交回复
热议问题