简体   繁体   English

无法跨域将jQuery POST请求发送到PHP服务器

[英]Cannot send jQuery POST request cross-domain to PHP server

I am unable to send POST requests using jQuery from my website (front-end) to my PHP Zend Server app. 我无法使用jQuery从我的网站(前端)向我的PHP Zend Server应用发送POST请求。 They exists as separate apps on my server and are configured to on two different domains: 它们在我的服务器上作为单独的应用程序存在,并配置为在两个不同的域上:

  • site.website site.website
  • site.server:8899 site.server:8899

The strangest thing is, it was working, I changed nothing (I think!), and now it doesn't work... So I'm hoping somebody can help. 最奇怪的是,它正在运行,我什么都没改变(我认为!),现在却不起作用了。所以我希望有人可以提供帮助。

I am using the following JavaScript to make the POST request: 我正在使用以下JavaScript发出POST请求:

var login = function(email, password) {

    var requestPath = serverPath + "/login";

    var params = {
        email       :       email,
        password    :       password
    };

    $.post(requestPath, params).done(function(response) {
        console.log(JSON.stringify(response));
        if(isResponseError(response)) {
            alert(response.opStatus + ': "' + response.opMessage + '"');
        } else {
            window.localStorage.setItem("zobyAuthData", JSON.stringify(response));
            if(window.localStorage.getItem("zobyAuthData") !== null) {
                window.location = 'home.php';
            }
        }
    }).error(function(request) { 
        console.log(JSON.stringify(request)); 
        alert(JSON.stringify(request));
    });

};

I am using the following code on my Zend Server to enable CORS in my index.php file within the public directory: 我在Zend Server上使用以下代码在公用目录下的index.php文件中启用CORS:

function EnableCors() {

    // Allow from any origin
    if (isset($_SERVER['HTTP_ORIGIN'])) {
        header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Max-Age: 86400');    // cache for 1 day
    }

    // Access-Control headers are received during OPTIONS requests
    if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {

        if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
            header("Access-Control-Allow-Methods: GET, POST, OPTIONS");         

        if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
            header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");

        exit(0);
    }

    echo "You have CORS!";
}

I have also check the Apache access_log and error_log files when I attempt to make the POST request and the following lines are written to access_log: 尝试发出POST请求时,我还检查了Apache access_log和error_log文件,并将以下行写入access_log:

127.0.0.1 - - [29/Jul/2013:12:00:37 +0100] "OPTIONS /login HTTP/1.1" 200 - 127.0.0.1--[29 / Jul / 2013:12:00:37 +0100]“ OPTIONS / login HTTP / 1.1” 200-

127.0.0.1 - - [29/Jul/2013:12:01:04 +0100] "POST / HTTP/1.1" 200 6611 127.0.0.1--[2013年7月29日:12:01:04 +0100]“ POST / HTTP / 1.1” 200 6611

And error_log has: 而且error_log具有:

[Mon Jul 29 11:47:36 2013] [error] [client 127.0.0.1] PHP Notice: Undefined index: email in /Library/WebServer/Documents/Site/WebsiteServer/application/controllers/LoginController.php on line 35, referer: http://site.website/ [2013年7月29日星期一11:47:36] [错误] [客户端127.0.0.1] PHP注意:未定义的索引:第35行的/Library/WebServer/Documents/Site/WebsiteServer/application/controllers/LoginController.php中的电子邮件,引荐来源: http//site.website/

[Mon Jul 29 11:47:36 2013] [error] [client 127.0.0.1] PHP Notice: Undefined index: password in /Library/WebServer/Documents/Site/WebsiteServer/application/controllers/LoginController.php on line 36, referer: http://site.website/ [2013年7月29日星期一11:47:36] [错误] [客户端127.0.0.1] PHP注意:未定义的索引:第36行的/Library/WebServer/Documents/Site/WebsiteServer/application/controllers/LoginController.php中的密码,引荐来源: http//site.website/

On the client, the jQuery falls into the .error(request) function and the following is displayed in an alert: 在客户端上,jQuery属于.error(request)函数,并且在警报中显示以下内容:

{"readyState":0,"responseText":"","status":0,"statusText":"error"} {“ readyState”:0,“ responseText”:“”,“状态”:0,“ statusText”:“错误”}

Yes, this will not work! 是的,这行不通! You are violating the same origin policy here. 您违反了此处的同一来源政策。 In order for this to work you have to use CORS Headers in your request. 为了使其正常工作,您必须在请求中使用CORS标头。 CORS is an HTML5-feature and it allows cross origin requests for XHR requests. CORS是HTML5的功能,它允许针对XHR请求的跨源请求。

Just visit these great sites for an intro on the topic: http://www.html5rocks.com/en/tutorials/cors/ or https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS?redirectlocale=en-US&redirectslug=HTTP_access_control 只需访问这些出色的网站,以获取有关该主题的介绍: http : //www.html5rocks.com/zh_CN/tutorials/cors/https://developer.mozilla.org/zh-CN/docs/HTTP/Access_control_CORS?redirectlocale = zh-CN&redirectslug = HTTP_access_control

EDIT: In order to debug set the following headers 编辑:为了调试设置以下标头

Header set Access-Control-Allow-Origin *
Header set Access-Control-Allow-Headers "content-type"

This should allow any page. 这应该允许任何页面。 Also don't forget the "Access-Control-Allow-Headers" field.. 另外,不要忘记“ Access-Control-Allow-Headers”字段。

Also use FF and Chrome for debuging. 还可以使用FF和Chrome进行调试。 Both give different errors on the same problem. 两者在同一问题上均给出不同的错误。 Depeding on the type of problem one or the other will give more information. 依靠一个或另一个问题的类型将提供更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM