• <tfoot id='PdHJI'></tfoot>
    <legend id='PdHJI'><style id='PdHJI'><dir id='PdHJI'><q id='PdHJI'></q></dir></style></legend>

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

    2. <small id='PdHJI'></small><noframes id='PdHJI'>

        <bdo id='PdHJI'></bdo><ul id='PdHJI'></ul>
      1. 当我可以从 PHP 发出请求时,为什么 Ajax 会给我一个跨源错误?

        Why does Ajax give me a cross origin error when I can make the request from PHP?(当我可以从 PHP 发出请求时,为什么 Ajax 会给我一个跨源错误?)
      2. <legend id='Pppqa'><style id='Pppqa'><dir id='Pppqa'><q id='Pppqa'></q></dir></style></legend>
        • <i id='Pppqa'><tr id='Pppqa'><dt id='Pppqa'><q id='Pppqa'><span id='Pppqa'><b id='Pppqa'><form id='Pppqa'><ins id='Pppqa'></ins><ul id='Pppqa'></ul><sub id='Pppqa'></sub></form><legend id='Pppqa'></legend><bdo id='Pppqa'><pre id='Pppqa'><center id='Pppqa'></center></pre></bdo></b><th id='Pppqa'></th></span></q></dt></tr></i><div id='Pppqa'><tfoot id='Pppqa'></tfoot><dl id='Pppqa'><fieldset id='Pppqa'></fieldset></dl></div>
            <tfoot id='Pppqa'></tfoot>

          1. <small id='Pppqa'></small><noframes id='Pppqa'>

              <tbody id='Pppqa'></tbody>
              <bdo id='Pppqa'></bdo><ul id='Pppqa'></ul>

                • 本文介绍了当我可以从 PHP 发出请求时,为什么 Ajax 会给我一个跨源错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我可以从 PHP 发出 GET 请求并获得正确的响应.这是我使用的功能:

                  PHP

                  function httpGet($url){$ch = curl_init();curl_setopt($ch,CURLOPT_URL,$url);curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);curl_setopt($ch,CURLOPT_HEADER, false);$output=curl_exec($ch);curl_close($ch);返回 $output;}

                  一个简单的例子:

                  $fakevalue='iamfake';$url="http://fakeurl.com?fakeparameter=".$fakevalue;$jsondata= httpGet($url);$fake_array = json_decode($jsondata, true);$weed_var=$fake_array['杂草'];//成功获得杂草.

                  该函数返回来自服务器的响应.

                  现在我在 AJAX 中尝试相同的 HTTP GET 请求,但我无法得到响应.

                  最初我认为问题出在我使用的 JavaScript 函数上.Google 为我提供了许多用于执行 HTTP GET 请求的 JavaScript 函数,但它们都有相同的问题.请求返回一个错误,而不是我在使用 PHP 时得到的数据.

                  JAVASCRIPT

                  var fakevalue = "iamfake";var fake_data = {假参数:假值};$.ajax({网址:http://fakeurl.com",数据:fake_data,类型:获取",跨域:真,数据类型:json",成功:功能(一){$("#getcentre").html(a);},错误:函数(){alert("失败了!");}});

                  JavaScript 错误

                  <块引用>

                  XMLHttpRequest 无法加载 http://fakeurl.com?fakeparameter=fakevalue.请求的资源上不存在Access-Control-Allow-Origin"标头.因此,不允许访问源http://localhost".`

                  我知道你会告诉我使用 CORS,但如果是因为没有Access-Control-Allow-Origin"标头,那么我如何在 PHP 中获得相同服务的响应?

                  解决方案

                  使用 PHP(或在您的服务器上运行的任何其他应用程序,或独立应用程序(包括作为浏览器扩展安装的应用程序)), 正在使用您的凭据(您的 cookie、您的 IP 地址、您的其他一切)从 Bob 的服务器请求数据.

                  使用 Ajax,要求 Alice 的浏览器使用 她的 凭据从 Bob 的服务器请求数据,然后将该数据提供给您的JavaScript(然后可以将其发送回您的服务器,以便您自己查看).

                  Bob 可能会给 Alice 提供不同的数据,然后他会给你.例如:Bob 可能正在运行 Alice 的电子银行系统或公司内部网.

                  因此,除非 Bob 的服务器告诉 Alice 的浏览器可以向您提供该数据(使用 CORS),否则浏览器将阻止您的 JavaScript 访问该数据.

                  有 CORS 的替代方案,但它们涉及使用并非设计为数据格式 (JSONP) 的文件类型分发数据(这也需要 Bob 的服务器合作)或让您的服务器从Bob,然后通过您服务器上的 URL(或像 YQL 那样两者的某种组合)使其可用(这意味着您获得的是 Bob 将提供给您的数据,而不是 Bob 将提供给 Alice 的数据).

                  I can make a GET request from PHP and get the correct response. This is the function I use:

                  PHP

                  function httpGet($url)
                  {
                      $ch = curl_init();
                  
                      curl_setopt($ch,CURLOPT_URL,$url);
                      curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
                      curl_setopt($ch,CURLOPT_HEADER, false);
                  
                      $output=curl_exec($ch);
                      curl_close($ch);
                      return $output;
                  }
                  

                  A simple example:

                  $fakevalue='iamfake';
                  $url="http://fakeurl.com?fakeparameter=".$fakevalue;
                  $jsondata= httpGet($url);
                  $fake_array = json_decode($jsondata, true);
                  $weed_var=$fake_array['weeds']; // successfully obtained weed.
                  

                  This function returns the response from the server.

                  Now I am trying the same HTTP GET request in AJAX, but I can't get the response.

                  Initially I thought the problem was with the JavaScript function that I use. Google provided with me lots of JavaScript functions for performing the HTTP GET request but they all had the same problem. The request returns an error instead of the data that I got when I used PHP.

                  JAVASCRIPT

                  var fakevalue = "iamfake";
                  
                  var fake_data = {
                      fakeparameter: fakevalue
                  };
                  
                  $.ajax({
                      url: "http://fakeurl.com",
                      data: fake_data,
                      type: "GET",
                      crossDomain: true,
                      dataType: "json",
                      success: function(a) {
                          $("#getcentre").html(a);
                      },
                      error: function() {
                          alert("Failed!");
                      }
                  });
                  

                  Error from JavaScript

                  XMLHttpRequest cannot load http://fakeurl.com?fakeparameter=fakevalue. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.`
                  

                  I know you are going to tell me to use CORS, but if it was because of the absence of 'Access-Control-Allow-Origin' header, then how did I get response for the same service in PHP?

                  解决方案

                  With PHP (or anything else running on your server, or a standalone application (including those installed as a browser extension)), you are requesting data from Bob's server using your credentials (your cookies, your IP address, your everything else).

                  With Ajax, you are asking Alice's browser to request data from Bob's server using her credentials and then to make that data available to your JavaScript (which can then send it back to your server so you can see it yourself).

                  Bob might give different data to Alice then he would give to you. For example: Bob might be running Alice's eBanking system or company intranet.

                  Consequently, unless Bob's server tells Alice's browser that it is OK to make that data available to you (with CORS), the browser will prevent your JavaScript from accessing that data.

                  There are alternatives to CORS, but they involve either distributing the data using a file type that isn't designed to be a data format (JSONP) (which also requires Bob's server to cooperate) or having your server fetch the data from Bob and then make it available through a URL on your server (or some combination of the two like YQL does) (which means that you get the data Bob will give to you and not the data Bob will give to Alice).

                  这篇关于当我可以从 PHP 发出请求时,为什么 Ajax 会给我一个跨源错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Appending GET parameters to URL from lt;formgt; action(将 GET 参数附加到来自 lt;formgt; 的 URL行动)
                  Forcing quot;Save Asquot; dialog via jQuery GET(强制“另存为通过 jQuery GET 对话框)
                  PHP - get certain word from string(PHP - 从字符串中获取某个单词)
                  How to debug a get request in php using curl(如何使用 curl 在 php 中调试 get 请求)
                  get a # from a url in php(从 php 中的 url 获取 #)
                  PHP - include() file not working when variables are put in url?(PHP - 将变量放入 url 时,include() 文件不起作用?)
                • <tfoot id='24hpI'></tfoot>

                    • <bdo id='24hpI'></bdo><ul id='24hpI'></ul>
                      1. <small id='24hpI'></small><noframes id='24hpI'>

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