<bdo id='ztZ26'></bdo><ul id='ztZ26'></ul>
  • <tfoot id='ztZ26'></tfoot>

    <small id='ztZ26'></small><noframes id='ztZ26'>

      <legend id='ztZ26'><style id='ztZ26'><dir id='ztZ26'><q id='ztZ26'></q></dir></style></legend>
        <i id='ztZ26'><tr id='ztZ26'><dt id='ztZ26'><q id='ztZ26'><span id='ztZ26'><b id='ztZ26'><form id='ztZ26'><ins id='ztZ26'></ins><ul id='ztZ26'></ul><sub id='ztZ26'></sub></form><legend id='ztZ26'></legend><bdo id='ztZ26'><pre id='ztZ26'><center id='ztZ26'></center></pre></bdo></b><th id='ztZ26'></th></span></q></dt></tr></i><div id='ztZ26'><tfoot id='ztZ26'></tfoot><dl id='ztZ26'><fieldset id='ztZ26'></fieldset></dl></div>
      1. 跨站 XMLHttpRequest

        • <bdo id='roZf7'></bdo><ul id='roZf7'></ul>

          1. <legend id='roZf7'><style id='roZf7'><dir id='roZf7'><q id='roZf7'></q></dir></style></legend>
            <i id='roZf7'><tr id='roZf7'><dt id='roZf7'><q id='roZf7'><span id='roZf7'><b id='roZf7'><form id='roZf7'><ins id='roZf7'></ins><ul id='roZf7'></ul><sub id='roZf7'></sub></form><legend id='roZf7'></legend><bdo id='roZf7'><pre id='roZf7'><center id='roZf7'></center></pre></bdo></b><th id='roZf7'></th></span></q></dt></tr></i><div id='roZf7'><tfoot id='roZf7'></tfoot><dl id='roZf7'><fieldset id='roZf7'></fieldset></dl></div>

                  <tbody id='roZf7'></tbody>

                  <small id='roZf7'></small><noframes id='roZf7'>

                  <tfoot id='roZf7'></tfoot>
                  本文介绍了跨站 XMLHttpRequest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!


                  我想提供一段 Javascript 代码,它可以在包含它的任何网站上运行,但它总是需要在托管 Javascript 的服务器上获取更多数据(甚至修改数据).我知道出于显而易见的原因存在安全限制.

                  I want to provide a piece of Javascript code that will work on any website where it is included, but it always needs to get more data (or even modify data) on the server where the Javascript is hosted. I know that there are security restrictions in place for obvious reasons.

                  考虑在 xyz.com 上托管的 index.html,其中包含以下内容:

                  Consider index.html hosted on xyz.com containing the following:

                  <script type="text/javascript" src="http://abc.com/some.js"></script>

                  some.js 能否使用 XMLHttpRequest 将数据发布到 abc.com?换句话说,abc.com 是否因为我们从那里加载了 Javascript 而被隐式信任?

                  Will some.js be able to use XMLHttpRequest to post data to abc.com? In other words, is abc.com implicitly trusted because we loaded Javascript from there?


                  some.js 能否使用 XMLHttpRequest 将数据发布到 abc.com?换句话说,abc.com 是否因为我们从那里加载了 Javascript 而被隐式信任?

                  Will some.js be able to use XMLHttpRequest to post data to abc.com? In other words, is abc.com implicitly trusted because we loaded Javascript from there?


                  No, because the script is loaded on to a seperate domain it will not have access...

                  如果您信任数据源,那么 JSONP 可能是更好的选择.JSONP 涉及将新的 SCRIPT 元素动态添加到页面,并将 SRC 设置为另一个域,并将回调设置为查询字符串中的参数.例如:

                  If you trust the data source then maybe JSONP would be the better option. JSONP involves dynamically adding new SCRIPT elements to the page with the SRC set to another domain, with a callback set as a parameter in the query string. For example:

                  function getJSON(URL,success){
                      var ud = 'json'+(Math.random()*100).toString().replace(/./g,'');
                      window[ud]= function(o){
                          var s = document.createElement('script');
                          s.type = 'text/javascript';
                          s.src = URL.replace('callback=?','callback='+ud);
                          return s;
                      var success = data.flag === 'successful';
                      if(success) {
                          alert('The POST to abc.com WORKED SUCCESSFULLY');

                  因此,您需要托管自己的脚本,该脚本可以使用 PHP/CURL 发布到 abc.com 域,然后以 JSONP 格式输出响应:

                  So, you'll need to host your own script which could use PHP/CURL to post to the abc.com domain and then will output the response in JSONP format:

                  我不太擅长 PHP,但可能是这样的:

                  I'm not too great with PHP, but maybe something like this:

                      /* Grab the variables */
                      $postURL = $_GET['posturl'];
                      $postData['name'] = $_GET['dataName'];
                      $postData['age'] = $_GET['dataAge'];
                      /* Here, POST to abc.com */
                      /* MORE INFO: http://uk3.php.net/curl & http://www.askapache.com/htaccess/sending-post-form-data-with-php-curl.html */
                      /* Fake data (just for this example:) */
                      $postResponse = 'blahblahblah';
                      $postSuccess = TRUE;
                      /* Once you've done that, you can output a JSONP response */
                      /* Remember JSON format == 'JavaScript Object Notation' - e.g. {'foo':{'bar':'foo'}} */
                      echo $_GET['callback'] . '({';
                      echo "'flag':' . $postSuccess . ',";
                      echo "'response':' . $postResponse . '})";

                  因此,您可以控制的服务器将充当客户端和 abc.com 之间的媒介,您将以 JSON 格式将响应发送回客户端,以便 JavaScript 可以理解和使用它...

                  So, your server, which you have control over, will act as a medium between the client and abc.com, you'll send the response back to the client in JSON format so it can be understood and used by the JavaScript...

                  这篇关于跨站 XMLHttpRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:可以使用 xhrFields 将 onprogress 功能添加到 jQuery. 下一篇:Javascript:覆盖 XMLHttpRequest.open()



                  1. <i id='f3yqu'><tr id='f3yqu'><dt id='f3yqu'><q id='f3yqu'><span id='f3yqu'><b id='f3yqu'><form id='f3yqu'><ins id='f3yqu'></ins><ul id='f3yqu'></ul><sub id='f3yqu'></sub></form><legend id='f3yqu'></legend><bdo id='f3yqu'><pre id='f3yqu'><center id='f3yqu'></center></pre></bdo></b><th id='f3yqu'></th></span></q></dt></tr></i><div id='f3yqu'><tfoot id='f3yqu'></tfoot><dl id='f3yqu'><fieldset id='f3yqu'></fieldset></dl></div>

                    <tfoot id='f3yqu'></tfoot>

                    <small id='f3yqu'></small><noframes id='f3yqu'>

                        <bdo id='f3yqu'></bdo><ul id='f3yqu'></ul>

                    1. <legend id='f3yqu'><style id='f3yqu'><dir id='f3yqu'><q id='f3yqu'></q></dir></style></legend>