开发者

Equivalent jQuery server call for my XMLHttpRequest is not working

开发者 https://www.devze.com 2023-03-14 04:02 出处:网络
I am using struts2 and jquery I am making a server request using XMLHttpRequest which works fine, I want an equivalent jquery solution

I am using struts2 and jquery I am making a server request using XMLHttpRequest which works fine, I want an equivalent jquery solution

function submitLogin()
{
    if(validate()) //this is true
    {
var url_action="/csm/login.action";
             var client; 
             var dataString;
             if (window.XMLHttpRequest){ 
                 client=new XMLHttpRequest();
             } else {                    
                 client=new ActiveXObject("Microsoft.XMLHTTP");
             }
             client.onreadystatechange=function(){
                 if(client.readyState==4&&client.status==200)
                 {

                 }
             };

             dataString="emailaddress="+document.getElementById("email_id").value+"&projectid="+document.getElementById("project_id").value;
             client.open("POST",url_action,true);
             client.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

             client.send(dataString);
}
}

The above works perfectly, so in jquery i wrote the below code, but it's not working, what might be the problem?

function submitLogin()
{
    if(vali开发者_JAVA技巧date()) //this is true
    {
        $.post("/csm/login.action",function(xml) {
            alert(xml); 
        });
    }
}


You're missing your dataString variable in your jQuery code. That can be easily done by invoking the data object:

$.post("/csm/login.action", {
    emailaddress:    $("#email_id").val(),
    projectid:       $("#project_id").val()
},function(xml) {
    alert(xml); 
});

jQuery will create the proper query string for you.

Ref.: $.post


ok then you need to send data as

 $.post("/csm/login.action",{emailaddess:"emailhere",projectid:'idHere'},function(xml) {
            alert(xml); 
        });

for more details on $.post

HTH


function submitLogin()
{
    if(validate()) //this is true
    {
        dataString="emailaddress="+$("#emailaddress").val()+"&projecti‌​d="+$("#project_id").val();
        $.post("/csm/login.action", dataString, function(xml) {
            alert(xml); 
        });
    }
}

Notice that you are not sending any data with your post, this probably the issue. I added the data part.


you are missing you dataString variable-

 function submitLogin() {    
     if(validate()) //this is true     {   
    dataString="emailaddress="+document.getElementById("email_id").value+"&projectid="+document.getElementById("project_id").value;
        url = "/csm/login.action"
        $.post(url ,dataString, function(xml) {             
       alert(xml);          
     });     
    } 
  }
0

精彩评论

暂无评论...
验证码 换一张
取 消