Ajax返回0,(没有互联网连接),即使使用Internet Access也是如此

时间:2017-08-03 19:22:25

标签: php jquery ajax

J-query Ajax调用在错误函数中返回0。通常在没有互联网连接时会返回0错误。即使我的互联网连接完全没问题。问题是它在朋友的机器上运行良好。在朋友的机器上,如果我关闭wifi,在加载页面后,因为它应该工作,它确实没有在他的机器上返回互联网连接。

$(document).ready(function(){
$("#forget_form").submit(function(event){
    event.preventDefault(); 
        $('#result').html('Processing...');
        $.ajax({
            url: '<?php echo base_url(); ?>Index/forget',
            type:'POST',
            data: {
                email: $("#email").val(),

            },
            success:function(data){
                        $('#result').html(data)     
            },
            error: function (jqXHR, exception) {
                        var msg = '';
                        if (jqXHR.status === 0) {
                            msg = 'There is no Internet connection.';
                        } else if (jqXHR.status == 404) {
                            msg = 'Requested page not found. [404]';
                        } else if (jqXHR.status == 500) {
                            msg = 'Internal Server Error Occured. [500].';
                        } else if (exception === 'parsererror') {
                            msg = 'Requested JSON parse failed.';
                        } else if (exception === 'timeout') {
                            msg = 'Request Time out reached.';
                        } else if (exception === 'abort') {
                            msg = 'Ajax request aborted.';
                        } else {
                            msg = 'Uncaught Error.\n' + jqXHR.responseText;
                        }
                        $('#result').html(msg);
            },
        });
});});

3 个答案:

答案 0 :(得分:3)

由于以下原因之一,可能会出现该问题。

1:请求正在通过系统范围代理进行阻止。

2:CORS问题(您可能正尝试从非www版本访问网站的www版本),请记住浏览器将www.example.com和example.com视为两个不同的网站。当您购买域名时,通常会同时获得两者,您只需要使用www或非www版本即可。

答案 1 :(得分:2)

如果您使用的是非www版本的网站,并且尝试访问www版本的网站,则jQuery可能返回错误0。 例如,您尝试在example.com的服务器上获取www.example.com。

答案 2 :(得分:2)

正如QBR SOFT所述,由于CORS(跨源请求共享)策略,如果您将内容托管在非www上并使用www版本进行请求,反之亦然,浏览器可能会阻止您的连接。 另外,如果您有Internet连接,请确保没有在Internet代理设置中设置系统范围的代理。