为什么HTTP GET从PHP的作品,但没有从AJAX? [英] Why HTTP GET works from PHP but not from AJAX?

查看:193
本文介绍了为什么HTTP GET从PHP的作品,但没有从AJAX?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我受够了这个奔忙。这不是我第一次做从PHP的HTTP GET请求。我已经做了很多次和放大器;已经得到每一次正确的反应。

这是PHP函数我利用一切时间。

PHP

 函数HTTPGET($网址)
{
    $ CH = curl_init();

    curl_setopt($ CH,CURLOPT_URL,$网址);
    curl_setopt($ CH,CURLOPT_RETURNTRANSFER,真正的);
    curl_setopt($ CH,CURLOPT_HEADER,假);

    $输出= curl_exec($ CH);
    curl_close($ CH);
    返回$输出;
}
 

一个简单的例子。

  $ fakevalue ='iamfake';
$ URL =htt​​p://fakeurl.com?fakeparameter=$ fakevalue。
$ jsondata = HTTPGET($网址);
$ fake_array = json_de code($ jsondata,真正的);
$ weed_var = $ fake_array ['杂草']; //成功获得了杂草。
 

该函数返回来自服务器的响应。瞧。 :) 现在,我想在AJAX相同的HTTP GET请求。但是,我没有得到回应。起初,我以为这是我使用的JavaScript函数的问题。谷歌提供了我很多的JavaScript函数用于执行HTTP GET请求。我想每一个'时间。但结果仍然相同。该请求返回一个错误的反应,而不是返回时,我用PHP,我得到了杂草。

JAVASCRIPT

  VAR fakevalue ='iamfake';
             VAR fake_data = {'fakeparameter:fakevalue};

             $阿贾克斯({
             网址:http://fakeurl.com,
             数据:fake_data,
             键入:GET,
             跨域:真正的,//启用此
             数据类型:JSON,
             成功:函数(响应){$('#getcentre)HTML(响应);
             },
             错误:函数(){警报(失败!); }
             });
 

错误在JavaScript

XMLHtt prequest无法加载http://fakeurl.com?fakeparameter=fakevalue。没有访问控制 - 允许 - 原产地标头的请求的资源present。原产地'的http:// localhost'的。因此没有允许访问

我知道你现在会说。 CORS。 CORS ???真??如果是因为缺乏访问控制 - 允许 - 原产地标题,然后我怎么得到响应相同的服务在PHP。谁能帮我?

 说一下我得到安宁的CORS'了。
             很抱歉,我无法得到响应。
             它继续这样....
             谢谢。格拉西亚斯。
 

解决方案

使用PHP,使用凭证(你的cookies Bob的服务器请求数据时,您的IP地址,你的一切)。

使用Ajax,爱丽丝的浏览器中使用的凭证请求来自Bob的服务器的数据,然后使这些数据提供给您

鲍勃可能会给出不同的数据爱丽丝那么他就会给你。例如:鲍勃可能运行Alice的电子银行系统或公司内部网。

因此​​,除非Bob的服务器告诉Alice的浏览器,这是确定以使数据提供给您(CORS),浏览器将prevent你的JavaScript访问这些数据。

有替代CORS,但它们涉及或者使用未设计成一个数据格式(JSONP),或者让你的服务器上获取鲍勃的数据,然后使其可通过URL文件类型分发数据你的服务器(或两者类似YQL做的某种组合)。

I am fed up with this sh*t. This ain't the first time I'm doing an HTTP GET request from PHP. I have done it many times & have got the correct response every time.

This is the PHP function I use every time.

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. Voila. :) Now I am trying the same HTTP GET request in AJAX. But I don't get the response. Initially I thought it's the problem of the JavaScript function that I use. Google provided with me lots of JavaScript functions for performing the HTTP GET request. I tried every one of 'em. But the result remains same. The request returns an error as the response instead of returning the weed 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, // enable this
             dataType: 'json',
             success: function(response ) { $('#getcentre').html(response);
             },
             error: function() { alert('Failed!'); }
             });

ERROR in 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 what you gonna say now. CORS. CORS??? Really?? 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. Can anyone help me??

             Say something I'm givin' up on CORS.
             Sorry that I couldn't get response.
             It goes on like that....
             Thanks. Gracias.

解决方案

With PHP, 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 you.

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

这篇关于为什么HTTP GET从PHP的作品,但没有从AJAX?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆