Jquery 向跨域发布数据并处理响应

Jquery 向跨域发布数据并处理响应,jquery,html,ajax,post,Jquery,Html,Ajax,Post,我是Jquery的新手,我正在尝试将一些数据发布到跨域,我想处理响应,这是一个完整的HTML页面 我正在使用以下代码 $.ajax({ url: "http://www.somehost.com/abc/xyz.php", type: "post", data:{page: "123", calltype: "1"}, // crossDomain: true, I tried with using it as well dataType:'html'

我是Jquery的新手,我正在尝试将一些数据发布到跨域,我想处理响应,这是一个完整的HTML页面

我正在使用以下代码

$.ajax({

    url: "http://www.somehost.com/abc/xyz.php",
    type: "post",
    data:{page: "123", calltype: "1"},
    // crossDomain: true,  I tried with using it as well
    dataType:'html',
    success: function(response, textStatus, jqXHR){
        console.log(response);
    },
    error: function(jqXHR, textStatus, errorThrown){
        console.log("The following error occured: "+textStatus);
        console.log(jqXHR);
    },
});
当我使用
数据类型:'html'
错误
函数被激发,firebug日志与

POST http://www.somehost.com/abc/xyz.php 302 Moved Temporarily 231ms
The following error occured: error
Object { readyState=0, status=0, statusText="error"}
但是,当我使用
数据类型:'jsonp'
时,再次启动了
error
函数,但这次成功加载了HTML响应,但出现了一些我无法理解的解析错误,并记录了firebug日志

POST http://www.somehost.com/abc/xyz.php 302 Moved Temporarily 231ms
The following error occured: error
Object { readyState=0, status=0, statusText="error"}
火虫

The following error occured: parsererror
Object { readyState=4, status=200, statusText="success"}
SyntaxError: syntax error       
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//E
发生以下错误:parsererror
对象{readyState=4,status=200,statusText=“success”}
语法错误:语法错误

如果希望响应是HTML而不是json,那么必须使用这样的代理

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="css/style.css"/>
<title>Welcome</title>
.
.
.
$.ajax({
url: 'proxy.php',
type: 'POST',
data: {
    address: 'http://www.somehost.com/abc/xyz.php',
    page: "123", 
    calltype: "1"
},
xhrFields: {
   withCredentials: true
},
success: function(response) {
    console.log(response);
},
error: function(jqXHR, textStatus, errorThrown){
    console.log(textStatus);
    console.log(errorThrown);
}
});
使用服务器端脚本

$postdata = http_build_query(
    array(
        'var1' => $_POST['page'],
        'var2' => $_POST['calltype']
    )
);

$opts = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => 'Content-type: application/x-www-form-urlencoded',
        'content' => $postdata
    )
);

$context  = stream_context_create($opts);

echo file_get_contents($_POST['address'], false, $context);
在.php文件中看到的所有内容都是因为您需要将参数页面和调用类型作为post请求传递

如果您想要接收html,JSONP方法需要帮助

这个答案是塔图乌尔曼语的混合体
还有pascal MARTIN

让我们看看响应标题。未启用愿意成为CORS。请参阅我的答案以在控制台上打印成功消息。也放一个错误回调函数。当我使用php代理时,它返回我的登录页面(意味着没有会话),但当我使用跨域调用时,它返回我想要的正确页面(但有一些我已经提到的解析错误)。你没有提到要保留会话。根据我的发现,您必须使用xhrFields:{withCredentials:true}并读取服务器脚本中的cookie并发送它。在服务器上检查$\u请求数组,并查看xhr参数是否正在发送凭据。然后将它添加到选项中,添加到标题中。这对我来说也是未知的领域。我要做的就是测试xhr。只需向test.php发出post请求,在那里返回$\u请求数组,查看cookie是否存在。如果是,那么您就知道它的结构,所以您可以像这样将它放在头选项中