Ajax NetworkError:发生网络错误 [英] Ajax NetworkError: A network error occurred

查看:828
本文介绍了Ajax NetworkError:发生网络错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就在昨天,这个代码在我的LAMP中启动并运行,我不确定是否可以造成任何重大变化。这是一个简单的ajax脚本,可以从API中检索数据。

Just yesterday this code was up and running in my LAMP, I'm not sure if I could inflict any major changes. It's a simple ajax script that retrieves data from API.

今天它停止了工作,控制台只是抛出NetworkError:发生网络错误。,甚至更奇怪的是,我自己尝试了格式化的GET查询,一切正常,API响应正确。

Today it stopped work, the console just throws "NetworkError: A network error occurred.", even stranger is that when I tried the formatted GET query myself it all worked fine, API responded correctly.

http://localhost/uzduotis2/data/mobile/api/mobile.php?query%5Boperators%5D%5B0%5D=1&query%5Boperators%5D%5B1%5D=2&query%5Boperators%5D%5B2%5D=3&query%5Bmin%5D=180&query%5Bsms%5D=400&query%5Bmb%5D=128&query%5Bperks%5D=7&_=1385898696657

好吧,我不知道还能做些什么 - 也许有办法让错误代码更具体?...无论如何,这里是代码:

Well, I don't know what else I can do—maybe there was a way to make the error code more specific?... Anyway, here's the code:

function ajaxCall(url, req, req_type, async) {

    return jQuery.ajax({
        type: req_type,
        url: url,
        data: req,
        contentType: "charset=utf-8",
        dataType: 'json',
        timeout: 30000,
        async: async,
        cache: false,
    });

}

 var ApiUrl = "http://localhost/uzduotis2/data/mobile/api/mobile.php";

$(document).ready(function() {

// .....

        var req = {query: {operators: Operators, min: Min, sms: Sms, mb: Mb, perks:Perks}};
        var promise = ajaxCall(ApiUrl, req, 'GET', false);

        promise.success(function(out) {
            console.log(out);
//            populate(out);
        });

        promise.error(function(out){
            console.log(out);
        });
}

编辑

我刚发现它适用于其他浏览器,必须是缓存相关问题,也许有人知道可能是什么?

I just found out that it works on another browser, must be a cache related issue, maybe somebody knows what could it be?

推荐答案

您必须以异步方式返回数据,因此必须将async设置为 true

you must return data in asynchrounous way so you must set async to true

var promise = ajaxCall(ApiUrl, req, 'GET', true);

这篇关于Ajax NetworkError:发生网络错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆