xmlHttpRequest.send(Params)在tomcat 7中不起作用

时间:2013-01-25 14:59:43

标签: javascript

我试图通过在JS文件中写入的xmlHttpRequest.send(params)向我的servlet发送一些参数,我尝试通过req.getParameter(“some_Parameter”)获取参数;它在servlet上返回null。虽然如果我通过在url中附加它们来发送参数,它可以正常工作。但是当网址很大时,它会破坏代码。所以请有人帮助我。

提前致谢。

function doHttpPost(theFormName, completeActivity) 
{ 
    var xmlhttp = new ActiveXObject("MSXML2.XMLHTTP"); 
    var xmlMessage = buildPOST(theFormName, completeActivity); 
    var responseTxt; 
    try { 
        xmlhttp.Open(document.forms[theFormName].method, document.forms[theFormName].action+'?'+xmlMessage, false); 
        xmlhttp.onreadystatechange=function() { 
            if (xmlhttp.readyState==4) { 
                responseTxt = xmlhttp.responseText; 
            } 
        } 
        xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        enableDisableLinks(true);
        setPointer();  
        xmlhttp.Send(); 
        if(xmlhttp.Status != 200) { 
            alert("Post to server failed"); 
        } 
    } catch (e) { 
        responseTxt = "Exception while posting form data: Error No: " + e.number + ", Message: " + e.description; 
    } 

    resetPointer();  
    enableDisableLinks(false);
    var expectedTxt = "Form Data had been successfully posted to the database." 
    if(responseTxt.toString() == expectedTxt.toString()) { 
        // MNP: New requirement from Jeanne, should not refresh CM page, commenting it off for now
        //if(completeActivity) {
        //  if (typeof (ViewCaseDetailBtn) != 'undefined') {
        //      ViewCaseDetailBtn.click();
        //  }
        //}
        return true; 
    } else {
        alert (responseTxt); 
    }
    return false; 
}

1 个答案:

答案 0 :(得分:1)

<强> BUGS

//IE only - shooting yourself in the
// Not all IE versions use ActiveX!
var xmlhttp = new ActiveXObject("MSXML2.XMLHTTP");    foot. 

//JavaScript case sensitive, open !== Open
xmlhttp.Open(document.fo...

//JavaScript case sensitive, send !== Send
xmlhttp.Send();

//JavaScript case sensitive, status !== Status
xmlhttp.Status

如果您使用的是同步,它不会调用onreadystatechange。

如果您使用的是POST,则该值必须位于send("valuestosendup")而不是查询字符串中。

此代码说明了为什么你应该真正使用框架来进行Ajax调用而不是自己动手。