1

I am developing an app in asp.net in which I have jquery code in my asp page

var strdata = {
           problemID: $("#problemID").val(),
           commentText: $("#_1").val(),
           empID: $("#empID").val(),
           agree: 0,
           disagree: 0
        };
        $.ajax({
            type: "POST",
            url: "<%= Url.Action("PostComment", "Discussion")  %>",
            data: strdata,
            dataType: "JSON",
            success: function (msg) {
                if ( msg == 1)
                alert("Success" + msg );
            }
        });

and my controller has code

  public bool PostComment(string problemID, string commentText, string empID, string agree, string disagree)
        {


            return _discussionRepository.postComment(Convert.ToInt32(problemID), commentText, Convert.ToInt32(empID), Convert.ToInt32(agree),Convert.ToInt32( disagree));
        }

and model has code

public bool postComment(int problemID, string commentText, int empID, int agree, int disagree)
        {
            bool result = false;
            Comment c = new Comment();
            c.ProblemID = problemID;
            c.CommentText = commentText;
            c.EmpID = empID;
            c.Agree = agree;
            c.DisAgree = disagree;

            _objectModel.Comments.InsertOnSubmit(c);
            try
            {
                _objectModel.SubmitChanges();
                result = true;

            }

            catch (Exception ex)
            {
            }


            return result;  

}

Data is saving in database through ajax and jquery but the success message is not showing

Snake
  • 337
  • 2
  • 7
  • 23

3 Answers3

1

If the alert is not running with or without the condition that means the datatype being returned is not the datatype the $.ajax function is expecting.

2 ways to get to the bottom of it:

  • First open up chrome or firebug and check out the network traffic. If you are getting the result back (the request is being made and the content looks accurate) then your data type is definitely the cause. Try changing the datatype in the request.

  • Next you could try adding functions other than just success. There is also error, status codes (404,500 etc), beforeSend, etc check the docs.

There are other ways as well :). Fiddler might help too.

Parris
  • 17,833
  • 17
  • 90
  • 133
  • Your datatype is "JSON", but I don't see you outputting json. Try wrapping your return statement with: var jsonSerializer = new System.Web.Script.Serialization.JavaScriptSerializer(); string json = jsonSerializer.Serialize(yourCustomObject); More info here: http://stackoverflow.com/questions/2287399/encode-object-to-json Also you may need to set headers for the page type to be "application/json" ... try not using json first perhaps? – Parris Jan 26 '12 at 20:22
0

To get the result of your AJAX request use the following code in your success handler:

success: function (msg) {
            if ( msg.d === true) {
              alert("Success" + msg.d );
            }
         }

You have to access the result via the "d" property.

To get the error message use the following error handler:

error: function(jqXHR, textStatus, errorThrown){
         alert(errorThrown);
       }
Hans
  • 12,902
  • 2
  • 57
  • 60
  • @Snake: Did you try using the error handler in your jquery AJAX call? What error code/error message do you get? – Hans Jan 26 '12 at 20:13
-1

i think the result returned in 'msg' isnt the number 1. maybe msg == true ?

Menahem
  • 3,974
  • 1
  • 28
  • 43