【问题标题】:jquery mobile ajax json errorjquery mobile ajax json错误
【发布时间】:2013-10-30 11:12:25
【问题描述】:

这个错误快把我逼疯了。

在使用 jquery 2.0.3 时它可以工作,当我更改为 jQuery 1.10.2 时它不会! 我需要使用 jQuery 1.10 才能使用 jquery 移动面板小部件 不知道怎么回事,json是合法的js我测试一下。

这是代码:(我更改了 html 以获取警报)

$(document).on('pageinit', "#news_list", function () {
    $.ajax({
        type: "GET",
        crossDomain: true,
        url: "http://app.virtual-competiciones.es/api/news/getnews?num=20",
        headers: { "Accept-Encoding": "gzip" },
        dataType: 'json',
        cache: false,
        success: function (data) {
            var result = data;
                        alert("ok");
        },
        error: function (xhr, status, error) {
            alert("ERROR - xhr.status: " + xhr.status + '\nxhr.responseText: ' + xhr.responseText + '\nxhr.statusText: ' + xhr.statusText + '\nError: ' + error + '\nStatus: ' + status);
        }

    });
    return false;
});

我得到的错误是未定义的(无传输)

【问题讨论】:

  • “这个错误快把我逼疯了” - 一定是你忘记告诉我们什么错误的原因……
  • 你用的是哪个jqm版本?
  • 我告诉你,它不起作用。错误在底部。

标签: jquery ajax json jquery-mobile


【解决方案1】:

我将回答我的问题,这样其他人就不必在同一件事上花费数小时了。

在使用 jQUery 2.0.3 时,除了我发布的代码之外,您不需要添加任何其他内容,如果您使用 jQuery 1.10.2,则需要添加以下内容:

<script>
$(document).on("mobileinit",function() {
    $.support.cors = true;
});        
</script>

在 jQuery 和 jQUery Mobile 脚本之间的 head 部分。 这允许进行跨域调用。

也许很简单,但因为我没有在 2.0.3 版本中使用它,所以对我来说并不是那么明显。 我希望这对其他人有所帮助。

【讨论】:

    【解决方案2】:

    我今天遇到了同样的问题。 Safari 浏览器 - 使用 Ipad - 未能提交表单并显示此错误消息:

    无法打开页面 Safari 无法打开页面,因为地址 无效

    我将 JQ 从 10.2 升级到 JQuery v2.0.3 (我也在使用 Jquery Mobile 1.4.0-rc.1)

    我一直在使用href="javascipt:void(0);" 作为空锚标签。这导致 JQ ajax 调用失败。

    <a href="javascipt:void(0);" onclick="submitDynamicForm('@rndId');" class="ui-btn ui-corner-all ui-shadow ui-btn-inline ui-icon-check ui-btn-icon-left ui-btn gl">Save</a>
    

    我删除了 href,它开始正常工作。它正在为 javascript:void(0); 发起另一个 http 调用。

    $.ajax({
        url: url,
        type: 'post',
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        //processData: false,
        data: jsonData,
        //crossDomain: true,
        headers: { "Timezone": getTimezone() },
        success: function (response) {
            console.log(response.data);
        },
        error: function (xhr, status, error) {
            //  console.log('ajax done - error');
            console.log("ERROR - xhr.status: " + xhr.status + '\nxhr.responseText: ' + xhr.responseText + '\nxhr.statusText: ' + xhr.statusText + '\nError: ' + error + '\nStatus: ' + status);
        }
    });
    

    【讨论】:

      猜你喜欢
      • 2011-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多