2012-07-08 44 views
1

如果我在+註釋符號中沒有提交變量記錄。有沒有什麼辦法可以在jQuery中編碼查詢字符串?我嘗試了一些方法,但他們沒有工作+在ajax查詢字符串中

$.ajax({ 
    type: 'post', 
    url: rootURL + 'services/service.php?method=insertcomment', 
    data: 'comment=' + comment+'&storyid='+storyid, 
    dataType: 'json', 
    success: function (data) { 
      if(data.code == 200) 
       $('#success-message')..show(); 
      else 
       alert('failure'); 
    } 
}); 

回答

1

您需要將數據編碼爲URL。

檢查相關的帖子:Encode URL in JavaScript?

或者將您的數據作爲JSON對象:

$.ajax({ 
    type: 'post', 
    url: rootURL + 'services/service.php?method=insertcomment', 
    data: {comment : comment, storyid : storyid}, 
    dataType: 'json', 
    success: function (data) { 
      if(data.code == 200) 
       $('#success-message').show(); 
      else 
       alert('failure'); 
    } 
}); 
+0

我同意,數據應該作爲一個對象傳遞。 – 2012-07-08 06:10:38