2012-07-09 73 views
0

大家好,我發送數據在後jQuery沒有AJAX後格式的問題。我有網址爲:

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"); 
} 


} 

這裏也是我得到正確的結果,但是當我用下面的方法後沒有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..... 

意味着特殊字符不編碼/解碼well.But我在上面又是什麼問題編碼呢?

+0

@dystroy如果您傳遞正確的用戶名和密碼,然後服務器返回一個字符串與響應如果其中一個或兩個都不正確,則編碼爲0.和-1。如果參數沒有以正確的格式發送,則它返回一個字符串,其響應代碼爲1.即成功,則字符串爲0 |^success | ^爲不正確的用戶名和密碼-1 |^Unsucessfull | ^和for third 1 |^Not an對象| ^。在我的情況下,我得到了最後的輸出 – PPD 2012-07-09 09:04:25

回答

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); 
+0

@dystory感謝您的解釋。但我仍然沒有得到它,你會請示出一些代碼 – PPD 2012-07-09 09:10:41

+0

現在我已經在提交之前添加表單到頁面。並在變量postParam中編碼參數。 – PPD 2012-07-09 09:27:05

+0

@dystory llok在我的代碼再次讓我知道我失蹤。謝謝 – PPD 2012-07-09 09:48:40