問題描述
ajax post 只看到第一個參數 (ajax post only sees first param)
這是我的 ajax 帖子:
$("#defaultMigration").click(function(){
$.ajax({
type: "POST",
url: "/Svn2GitService/services/svn2git/defaultMigration",
contentType: 'application/json',
data: JSON.stringify({ svnUrl : $("#svnUrl").val(),
gitUrl : $("#gitUrl").val(),
username : $("#username").val(),
password : $("#password").val()
}),
dataType: 'text',
success: function(response){
alert("Success!" + response);
},
failure: function(response) {
alert("Error! " + response);
}
});
});
當我在服務中執行 System.out.println
時,我看到 svnUrl
param 是唯一收到任何東西的人。事實上,它正在接收上述所有值。即svn url、git url、用戶名和密碼。
為什麼會這樣?
參考解法
方法 1:
Just out of curiosity, try the following. If it works, I'll explain what has happened after.
I'm using jQuery's $.param() function to serialize the data
object.
$.param()
Create a serialized representation of an array, a plain object, or a jQuery object suitable for use in a URL query string or Ajax request.
var data = {
svnUrl : $("#svnUrl").val(),
gitUrl : $("#gitUrl").val(),
username : $("#username").val(),
password : $("#password").val()
}
data = $.param(data);
$.ajax({
type: "POST",
url: "/Svn2GitService/services/svn2git/defaultMigration",
contentType: 'application/json',
data: JSON.stringify(data),
dataType: 'text',
success: function(response){
alert("Success!" + response);
},
failure: function(response) {
alert("Error! " + response);
}
});
(by Chris Bolton、TheCarver)