【问题标题】:ajax jsonp request - no responseajax jsonp 请求 - 无响应
【发布时间】:2012-06-06 13:28:13
【问题描述】:

首先喜欢这个资源 - 使用它并学习了几年。这是我的第一篇文章,因为我真的被困住了。我正在通过 ajax /jsonp 提交表单。如果我在本地运行此脚本 - 我会从域中收到带有成功代码的响应。如果我只是在浏览器中运行请求,它会给我回复成功代码。但是当我提交表单时,Firebug 在 RED 中给了我一个 200 OK,而服务器没有响应。 Safari 给我一个加载资源失败:取消。找不到太多关于错误的文档,所以我停了下来。我知道这对于你的专业人士来说可能非常恶心,但这是我的第一篇文章,所以任何指导都非常感谢!网上有两个例子:http://www.yourlifeportal.com/register.php,里面有reCaptcha的版本。 http://www.yourlifeportal.com/registerNew.php 没有 reCaptcha 以防添加验证码影响了我的代码。如果我只是需要一个耳光,也请告诉我。谢谢!

$.ajax({
        url: 'http://myURLonaDifferentDomain',
        data:jQuery(frm).serialize(),
        type: 'POST',
        dataType: 'jsonp',
        jsonp: 'jsonp',
        crossDomain: true,
        error: function (xmlHttpRequest, textStatus, errorThrown) {
                    if(xmlHttpRequest.readyState == 0 || xmlHttpRequest.status == 0) 
          return;  // it's not really an error
     else
                alert(xmlHttpRequest + ': ' + textStatus + ': ' + errorThrown);
                    },
        success: function(jsonp) { 
            // Response handling code goes here
            console.log(json.response.responseCode + ': ' + json.response.response + ': ' + json.response.responseDescription);

            if (json.response.responseCode == 10527) { 
            document.getElementById('errorScreen').style.display='block';
            $('#errorMsg').append('There was an error with your credit card transaction please go back and re-check your ');

            } 
                    if (json.response.responseDescription == "Registration was successful") {

            window.location.replace("http://www.url.com/thankyou.php");             
            } 

        }
                        });


}

【问题讨论】:

    标签: jquery ajax jsonp


    【解决方案1】:

    哈哈哈。跨域脚本。这是个大问题。阅读解决方案here

    编辑我重新阅读了这个问题并注意到您已经为跨域准备了 AJAX(尽管它通常会自己注意到);几乎可以肯定,问题在于您没有准备好远程 Web 服务器。使用 Firebug,打开 Net 选项卡,然后查看特征 CORS 标头的响应标头。

    【讨论】:

    • 我还以为jsonP是解决跨域问题的?
    • 这是 Firebug 为响应标头显示的内容。我错过了什么吗?日期 Sun,2011 年 8 月 21 日 07:15:50 GMT 服务器 Apache/2.2.8 (CentOS) Content-Language en-US Content-Length 128 Connection close Content-Type application/x-javascript;charset=UTF -8
    • 是的,没有 CORS 标头(看起来像 Access-Control-Allow-Origin)。您可以查看请求是否看起来像 JSOP 请求(在 GET 中会有一个参数,例如 jsonp=xyz)以及响应是否正确(它将以 xyz(... 开头)
    【解决方案2】:

    我遇到了类似的问题,解决方案是 JSONP 响应必须包含在回调函数中。在这里回答:https://stackoverflow.com/a/10892749/498903

    【讨论】:

      猜你喜欢
      • 2017-06-20
      • 2016-06-04
      • 2016-08-17
      • 2019-01-22
      • 2013-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-13
      相关资源
      最近更新 更多