【问题标题】:Accessing Server responce in jquery ajax error handler在 jquery ajax 错误处理程序中访问服务器响应
【发布时间】:2018-08-15 12:28:33
【问题描述】:

我正在跨域尝试 ajax。我总是得到错误代码0。我搜索了很多,发现这是由于服务器标头的限制。

当我在 Firebug 上检查 ajax 时,我可以看到来自服务器的响应。所以只有我的问题是我需要在错误处理程序中获得响应而不是成功。但我试图在错误处理程序中打印响应,但我变得空了

error: function (xhr, status, error) {
         alert(xhr.responseText);

这是我的ajax函数的样子

<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
$.ajax({
        type: "POST",
        url: "externam_url",
        crossDomain: true,
        data: 'somedata',
        success: function (data) {
        alert(data);
            // do something with server response data
        },
        error: function (xhr, status, error) {
         alert(xhr.responseText);

    }

所以请帮助我。我的 ajax 工作得很好,但是如何在“错误”处理程序中读取 ajax 响应数据

【问题讨论】:

标签: jquery ajax cross-domain


【解决方案1】:

切换到 Curl 解决方案。

$data = array('name' => 'Foo', 'id'=>'Bar'); 
    curl_setopt($ch, CURLOPT_URL, 'external_url');
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);     

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-13
    • 2012-03-24
    • 1970-01-01
    • 2014-01-13
    • 2011-11-04
    • 2017-05-25
    相关资源
    最近更新 更多