duanke2012 2013-05-14 21:31
浏览 78

ajax http请求状态仅在移动浏览器上返回零

I have been using this code for a long time successfully:

function doXMLReq(url, data) {
    var req = false;
    if (window.XMLHttpRequest) { 
        req = new XMLHttpRequest();
        if (req.overrideMimeType) {
            req.overrideMimeType('text/xml');
        }
    }
    req.onreadystatechange = function() { doStuff(req); };
    req.open('POST', url, true);
    req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    req.setRequestHeader('Content-length', data.length);
    req.setRequestHeader('Connection', 'close');
    req.send(data);
}

function doStuff(req) {
    if (req.readyState == 4) {
        alert(req.status);
        if (req.status == 200) {
            alert('ok');
        }
    }
}

The URL submits a post to a PHP script which returns XML. I added in these alerts to do simple testing. Obviously where the alerts are major functionality is placed, and on every desktop browser I reach the "ok" alert after submitting my form. Additionally, if console.log(xml) type statements are issued, I can see that the XML is loaded properly on the desktop browser, and functionality is normal.

For some reason on mobile browsers (safari, android) the req.status alert always has a value of 0, and the XML data is not returned. If I post a form directly to the URL (instead of using this JS), the browser renders the XML perfectly.

The URL is in the same directory as the original PHP file, on the same domain, etc. The header is set correctly on the PHP form which is passed through URL.

header('Content-type: text/xml');

I am thinking that it may have something to do with https, since this is the only page which encounters this error, and it is also the only page in the https domain out of maybe a hundred pages this code is on.

I have also tried to use jQuery's $.ajax, because many other users in other threads have suggested that the use of jQuery could solve cross-browser woes. I re-wrote my entire PHP script and much of my javascript to accomidate for $.ajax and I must admit I reduced the code by a meaningful amount, but the XML HTTP request still returns 0 on mobile browsers... here is the code I used:

function doForm() {
    var form = $('#i_form');
    $.ajax({
            type: form.attr("method") || 'post',
            url: "fooscript.php",
            data: form.serialize(),
            dataType: 'xml', //Disables xml autoparsing
            success: function(xml){
                    doXMLback(xml);
            },
            error: function(jqXHR, textStatus, errorThrown) {
                    alert(jqXHR.status);
                    alert(jqXHR.statusText);
            }
    });
    return false; 
}

function doXMLback(xml) {
    var errors = $(xml).find("errors").text();
    var success = $(xml).find("uri").text();
    if (errors) {
        errmsg = errors.split("|");
        errmsg = errmsg[1];
        $('#er').text(errmsg);
        return false;
    }
    if (success) {
        window.location = success;
    }
} 

(Obviously above instead of the form being submitted there is a div which has a simple onclick function which calls the doForm() function)...

How can I get mobile browsers running on 3G/4G to submit to a form and receive XML data back using ajax?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
    • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
    • ¥20 matlab yalmip kkt 双层优化问题
    • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
    • ¥88 实在没有想法,需要个思路
    • ¥15 MATLAB报错输入参数太多
    • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
    • ¥15 有赏,i卡绘世画不出
    • ¥15 如何用stata画出文献中常见的安慰剂检验图
    • ¥15 c语言链表结构体数据插入