简体   繁体   English

post方法数据在jquery mobile中未正确发送

[英]post method data is not sending correctly in jquery mobile

Hi I want to send data to server using post method but not using ajax I am sending data like this: 嗨,我想使用post方法将数据发送到服务器,但不使用ajax来发送数据,如下所示:

function handleLogin()
{

var form = $("#loginForm");    
 var u = $("#username", form).val();
var p = $("#password", form).val();
var d = $("#dob", form).val();

if(u != '' && p!= '')
{

    var finalStr = u+encodeURIComponent("|^")+p+encodeURIComponent("|^")+encodeURIComponent("|^")+"X"+encodeURIComponent("|^")+d+encodeURIComponent("|^")+"1.0"+encodeURIComponent("|^|$");
    var encodedURL = encodeURI(intranetUrl+"customer/Ri_logon5.asp?requestString=");
    var parameters =  decodeURIComponent(finalStr);
    post_to_url(encodedURL,parameters);      

}


else
{

    alert("You must enter a username and password", function() {});
    $("#submitButton").removeAttr("disabled");
}


}

and my post_to_url function is as: 而我的post_to_url函数是:

function post_to_url(url, params) {
var form = document.createElement('form');
form.action = url;
form.method = 'POST';

for (var i in params)
{
    if (params.hasOwnProperty(i))
    {
        var input = document.createElement('input');
        input.type = 'hidden';
        input.name = i;
        input.value = params[i];
        form.appendChild(input);                      
    }
}

 form.submit();
}

But from server I got response not an object which comes when u pass wrong parameter. 但是从服务器我得到的响应不是当您传递错误的参数时出现的对象。 But when I pass same parameter with ajax ten it works perfectly. 但是,当我用ajax 10传递相同的参数时,它可以很好地工作。 why is it so? 为什么会这样呢?

my url format is:http://myDomain/Ri_logon5.asp?requestString= 我的网址格式是:http://myDomain/Ri_logon5.asp?requestString =

and parameter format is like this:manish|^info1234|^|^X|^11111985|^1.0|^|$ 并且参数格式如下:manish | ^ info1234 | ^ | ^ X | ^ 11111985 | ^ 1.0 | ^ | $

If I enter url and pass parameter in rest client then it give proper response. 如果我输入url并在rest client中传递参数,那么它会给出正确的响应。

Seeing as you want to use POST instead of GET, you should change youre code from passing multiple post values. 好像要使用POST而不是GET一样,您应该通过传递多个post值来更改您的代码。 What you want is to only post requestString. 您只想发布requestString。 Also you should change the url to: myDomain/Ri_logon5.asp 另外,您应该将URL更改为:myDomain / Ri_logon5.asp

function post_to_url(url, params) {
var form = document.createElement('form');
form.action = url;
form.method = 'POST';

// Change this to fetch the arguments and build the string accordingly
var postString = 'manish|^info1234|^|^X|^11111985|^1.0|^|$'; 

var input = document.createElement('input');
input.type = 'hidden';
input.name = 'requestString';
input.value = postString
form.appendChild(input);  

 form.submit();
}

I had used following mwthod in ajax which works fine: 我在ajax中使用了以下mwthod,效果很好:

$.ajax({
                            type: "POST",
                            contentType:"application/x-www-form-urlencoded; charset=UTF-8",
                            url: clientDetailURL,
                            data: finalclientDetailParam
                          }).done(function( msg1 )
                          {
                                      var clientDetailResponse = msg1;
                                      console.log("Client detail response is:"+clientDetailResponse);


                          });

and here clientDetailURL is:http://myDomain/Ri_logon5.asp?requestString= and finalClientDetailParam are: manish|^info1234|^|^X|^11111985|^1.0|^|$ 这里的clientDetailURL是:http://myDomain/Ri_logon5.asp?requestString =和finalClientDetailParam是:manish | ^ info1234 | ^ | ^ X | ^ 11111985 | ^ 1.0 | ^ | $

and for this ajax it works fine but same not for POST method without ajax. 对于这个ajax,它可以正常工作,但对于没有ajax的POST方法,则不能正常工作。 It is totally confusing. 这完全令人困惑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM