$ .post和$ .ajax POST工作完美但不简单

时间:2012-07-09 08:57:31

标签: javascript jquery jquery-ui jquery-mobile

大家都知道我在没有ajax的jquery中以post格式发送数据时遇到了问题。我的网址为:

intranetUrl+"customer/Ri_logon5.asp?requestString=";

并希望发送参数为:

'manish|^info1234|^|^X|^11111985|^1.0|^|$'; 

我用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);
    alert("param:"+parameters);

    $.post(encodedURL, parameters ,
            function(data) {
              alert("Data Loaded: " + data);
            });

}


else
{

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

}

我得到了正确的结果。

当我用它时:

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);
    alert("param:"+parameters);

 $.ajax({
        type: "POST",
        contentType:"application/x-www-form-urlencoded; charset=UTF-8",
        url: encodedURL,
        data: parameters
      }).done(function(msg)
              {
                  response  = msg
                  console.log("repon s???????????????e::>::"+response);
          });

}


else
{

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


}

这里我也得到了正确的结果但是当我使用以下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);
    alert("param:"+parameters);
    post_to_url(intranetUrl+"customer/Ri_logon5.asp", finalStr); 




}


else
{

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


}


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

var postParam = encodeURIComponent(params);

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

}

但是使用这最后一个代码我没有获得所需的输出。这有什么不对?任何建议将不胜感激。

现在我检查了哪个服务器得到了输出它得到了:

requestString%3D=manish%25257C%25255Einfo1234%25257C.....

意味着特殊字符不能很好地编码/解码。但我在上面对它们进行了编码然后出现了什么问题?

1 个答案:

答案 0 :(得分:1)

第三种方式存在一些问题:

  • 您不应将整个URI设置为操作,而只应设置为intranetUrl+"customer/Ri_logon5.asp"

  • 您不应将输入字段的值设置为URIEncoded字符串。保留表单提交进行编码。

  • 它离开了页面,因为这是form.submit();

  • 的正常比例
  • 您应该在提交前将表单添加到页面中:document.body.appendChild(form);

前两点意味着你应该用

调用你的函数
var finalStr = u+encodeURIComponent("|^")+p+encodeURIComponent("|^")+encodeURIComponent("|^")+"X"+encodeURIComponent("|^")+d+encodeURIComponent("|^")+"1.0"+encodeURIComponent("|^|$");
post_to_url(intranetUrl+"customer/Ri_logon5.asp", finalStr);