Question

I know this has been asked many times before, but after reading all the solutions, I still can't figure out a way to solve this problem. I am stuck at this point from last 4 hours, so any help will really be appreciated. Here is my ajax call:

       $.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 code:

[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;
    }

}
Was it helpful?

Solution

In your case first you will have to fill the object before passing using ajax as shown below

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);
            }
        });

OTHER TIPS

you need to some change on your ajax methods ..

 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);
            }
        });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top