2012-07-08 58 views
-1

您好我想發送數據到服務器在post方法。我知道這是一個簡單的問題,但我沒有得到很好的文件。我有發送數據到服務器的方法,但它使用ajax。我想發送數據沒有Ajax如何做到這一點?這是我的代碼。發送數據到服務器沒有ajax

$.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); 
       }); 
+4

ajax有什麼問題? – Onheiron 2012-07-08 08:16:42

+4

_'send data without ajax ...'_ like a form submit for post,or url redirect for a get? – nbrooks 2012-07-08 08:17:19

+0

@ OnheironHi我想在手機上開發這個應用程序。它必須支持黑莓和bada。但是在黑莓版本5中,它不支持ajax。在版本6中它工作正常 – PPD 2012-07-08 08:18:06

回答

0

OK現在我張貼的數據是這樣的:

function post_to_url(path, params, method) { 

console.log ("Inside post_to_url>>>>>>>>"); 
method = method || "post"; // Set method to post by default, if not specified. 

// The rest of this code assumes you are not using a library. 
// It can be made less wordy if you use one. 
var form = document.createElement("form"); 
form.setAttribute("method", method); 
form.setAttribute("action", path); 

for(var key in params) { 
    if(params.hasOwnProperty(key)) { 
     var hiddenField = document.createElement("input"); 
     hiddenField.setAttribute("type", "hidden"); 
     hiddenField.setAttribute("name", key); 
     hiddenField.setAttribute("value", params[key]); 

     form.appendChild(hiddenField); 
    } 
} 

document.body.appendChild(form); 
form.submit(); 

}

,並調用此方法爲:

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_logon.asp?requestString="); 
    var parameters = decodeURIComponent(finalStr); 
    console.log("before calling post_to_url method"); 
    post_to_url(encodedURL,parameters,'post'); 




} 


else 
{ 

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


} 

但現在我正在從服務器輸出:「不是一個對象」。它來自服務器,當參數paased或特殊字符不完美編碼。但是我的編碼參數很好,就像我上面的原始問題。那爲什麼這樣呢?

+0

來源:https://stackoverflow.com/questions/133925/javascript-post-request-like-a-form-submit/133997#133997 – 2017-11-22 16:17:28