我知道之前已经多次询问过,但在阅读完所有解决方案之后,我仍然无法找到解决这个问题的方法。从最近4个小时开始我就陷入困境,所以任何帮助都会受到赞赏。 这是我的ajax电话:
$.ajax({
url: "http://localhost:1316/MoviesWebService.asmx/saveReview",
data: '{ username:"kartik1990",critic:"sanjayR",quote:"great",imdbId:"tt189",isCriticAMember:"true",reviewOrLikes:"review" }',
type: 'post',
contentType: 'application/json',
dataType:'json',
success: function (response) {
console.log(response);
}
});
ASMX代码:
[WebMethod]
public bool saveReview(ProjLikesTO newReviewOrLike) {
using (kkapilaCSDataContext db = new kkapilaCSDataContext()) {
proj_like action = new proj_like();
action.username = newReviewOrLike.username;
action.critic = newReviewOrLike.critic;
action.quote = newReviewOrLike.quote;
action.imdbId = newReviewOrLike.imdbId;
action.isCriticAMember = newReviewOrLike.isCriticAMember;
action.reviewOrlikes = newReviewOrLike.reviewOrLikes;
db.proj_likes.InsertOnSubmit(action);
db.SubmitChanges();
return true;
}
ProjLikesTO:
public class ProjLikesTO {
public string username {
set;
get;
}
public string critic {
set;
get;
}
public string quote {
set;
get;
}
public string imdbId {
set;
get;
}
public string isCriticAMember {
set;
get;
}
public string reviewOrLikes {
set;
get;
}
public ProjLikesTO() {
}
public ProjLikesTO(string username, string critic, string quote, string imdbId, string isCriticAMember, string reviewOrLikes) {
this.username = username;
this.critic = critic;
this.quote = quote;
this.imdbId = imdbId;
this.isCriticAMember = isCriticAMember;
this.reviewOrLikes = reviewOrLikes;
}
}
答案 0 :(得分:0)
在你的情况下,首先你必须在使用ajax传递之前填充对象,如下所示
var newReviewOrLike = { username:"kartik1990",critic:"sanjayR",quote:"great",imdbId:"tt189",isCriticAMember:"true",reviewOrLikes:"review" }
$.ajax({
url: "http://localhost:1316/MoviesWebService.asmx/saveReview",
data: JSON.stringify({ newReviewOrLike : newReviewOrLike }),
type: 'post',
contentType: 'application/json',
dataType:'json',
success: function (response) {
console.log(response);
}
});
答案 1 :(得分:0)
您需要对ajax方法进行一些更改..
var newReviewOrLike= { username:"kartik1990",critic:"sanjayR",quote:"great",imdbId:"tt189",isCriticAMember:"true",reviewOrLikes:"review" };
$.ajax({
url: "http://localhost:1316/MoviesWebService.asmx/saveReview",
data: JSON.stringify({'newReviewOrLike':newReviewOrLike}), // check this
type: 'post',
contentType: 'application/json',
dataType:'json',
success: function (response) {
console.log(response);
}
});