2013-12-19 99 views
1

我是新來的jQuery AJAX在這裏這段代碼我採取的形式參數,然後我希望他們能以JSON格式..here解析的代碼是如何JSON格式解析數據

 function doAjaxPost() { 
     var name = $('#name').val(); 
     var password = $('#password').val(); 
     var gender = $('#gender').val(); 
     var aboutYou = $('#aboutYou').val(); 


     $.ajax({ 
      type: "POST", 
      contentType:'application/json', 
      url: "add.htm", 
      dataType:'json', 
      data: "name=" + name + "&password=" + password + "&gender=" + gender + "&aboutYou=" + aboutYou, 
      success: function(data){ 
      alert('name : '+name); 
      }, 
      error: function(e){ 
      alert('Error: ' + e); 
    } 
    }); 
} 

這裏我的問題是我不是JSON格式得到我的數據並在此之後我必須發送JSON數據控制器add.htm ....在此先感謝

回答

0

現在,它的工作:)

function doAjaxPost() { 
    var name = $('#name').val(); 
    var password = $('#password').val(); 
    var gender = $('#gender').val(); 
    var aboutYou = $('#aboutYou').val(); 
    var your_data ={name: name ,password:password, gender: gender ,aboutYou:  aboutYou}; 


    $.ajax({ 
     type: "POST", 
     contentType:'application/json', 
     url: "add.htm", 
     dataType:'json', 
     data:your_data , 
     success: function(data){ 
     alert('name : '+name); 
     }, 
     error: function(e){ 
     alert('Error: ' + e); 
} 
}); 

}

4

錯過{}
變化=:

data: {"name" : name , "password" : password , "gender" : gender , "aboutYou" : aboutYou} 
+1

也錯過了 「」=> { 「名」:TOTO密碼:TOTO}應該是{ 「名」 :託託,「密碼」:託託} – BironDavid

+0

@BironDavid謝謝我已修復它:) – Praveen

0

你可以試試這個:

$.ajax({ 
      type: "POST", 
      contentType:'application/json', 
      url: "add.htm", 
      dataType:'json', 
      data:{name:name,password:password,gender:gender,aboutYou:aboutYou}, 
      success: function(data){ 
      alert('name : '+name); 
      }, 
      error: function(e){ 
      alert('Error: ' + e); 
    } 
    }); 

您可以在服務器端retireve那些PARAMATERS ..

1

我個人使用JSON.Stringify

function doAjaxPost() { 
     var name = $('#name').val(); 
     var password = $('#password').val(); 
     var gender = $('#gender').val(); 
     var aboutYou = $('#aboutYou').val(); 

     var parameters = 
    { 
     name: name, 
     password: password, 
     gender: gender, 
     aboutYou: aboutYou 
    }; 


    $.ajax({ 
     type: "POST", 
     contentType:'application/json', 
     url: "add.htm", 
     dataType:'json', 
     data: JSON.stringify(parameters), 
     success: function(data){ 
     alert('name : '+name); 
     }, 
     error: function(e){ 
     alert('Error: ' + e); 
} 

});
}

1
function doAjaxPost() { 
    var name = $('#name').val(); 
    var password = $('#password').val(); 
    var gender = $('#gender').val(); 
    var aboutYou = $('#aboutYou').val(); 

    $.ajax({ 
     type: "POST", 
     contentType:'application/json', 
     url: "add.htm", // probably something like add.php!? 
     dataType:'json', 
     data: { 
      name: name, 
      password: password, 
      gender: gender, 
      aboutYou: aboutYou 
     }, 
     success: function(data){ 
      console.log('success', data); 
      alert('name : '+name); // this is probably wrong - you already know the name 
     }, 
     error: function(e){ 
      console.log('error', arguments[0], arguments[1], arguments[2]); 
      alert('Error: ' + e); 
     } 
    }); 
}