跨域从Javascript到PHP

跨域从Javascript到PHP,javascript,php,jquery,cross-domain,Javascript,Php,Jquery,Cross Domain,我必须从服务器a上的javascript向服务器B上的php文件发出请求。我可以访问这两个服务器 但有点不对劲。我总是准备好状态0,状态0 这是我最近尝试的一件事:请告诉我我做错了什么。谢谢 服务器A: $.ajax({ type: "GET", url: 'http://server_B/request.php', data: form_data, dataType: 'json', success: function (resp) {

我必须从服务器a上的javascript向服务器B上的php文件发出请求。我可以访问这两个服务器

但有点不对劲。我总是准备好状态0,状态0

这是我最近尝试的一件事:请告诉我我做错了什么。谢谢

服务器A:

$.ajax({
    type: "GET",
    url: 'http://server_B/request.php',
    data: form_data,
    dataType: 'json',
    success: function (resp) {
        alert("Successful");
        console.log("Response completed");
        console.log("resp is" + resp);
    },
    error: function (xhr, error) {
        console.log("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
        console.log("responseText: " + xhr.responseText);
        alert("Error occurred.");
    }
});
服务器B:request.php


要在两个不同的域之间使用ajax,您必须使用


您可以在

中找到更多帮助,我不熟悉jsnop,我在控制台上也没有得到任何结果,因此无法了解发生了什么。在ajax调用中,使用数据类型:“jsonp”-了解您是否从控制台中登录的XMLHttpRequest中得到了安全性错误?如果您允许
*
,它是否工作?您在控制台中看到了什么?正在发送什么特定请求?它是一个选项还是一个GET?您的javascript控制台中应该有一个错误。在Chrome中(可能还有其他的,但肯定是Chrome),它会准确地告诉你问题出在哪里。这更适合作为评论。但是,
每个人都有自己的
“你必须使用jsonp”-不,你也可以使用CORS,这正是OP试图做的。
<?php
    header('Content-Type: application/json');
    header('Access-Control-Allow-Origin: '.$_SERVER['HTTP_ORIGIN']);
    header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
    header('Access-Control-Max-Age: 1000');
    header('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With');

    # do the work and save it on $result array.
    print json_encode($result,true);
?>