imdadhusen
imdadhusen

Reputation: 2494

JQuery not return any status like Success, Failed, Completed

Experts,

The following function are successfully send data to the Controller.cs (server)

 var ajaxResponse = $.ajax({
                        type: "post",
                        url: href,
                        dataType: 'json',
                        contentType: "application/json; charset=utf-8",
                        data: JSON.stringify(postData),
                        success: function (msg) {
                           alert("Success");
                        }
                    })

but i am not able to get updated status from the following method

[AcceptVerbs(HttpVerbs.Post)]
        public ActionResult SaveData(TabMasterViewModel postData)
        {
            string message = "";
            try
            {
                TabMasterViewModel update = new TabMasterViewModel();
                update = _tabmasterService.GetSingle(r => r.colID == postData.colID);
                UpdateModel(update);
                _tabmasterService.Update(update);
                message = "Success";
            }
            catch (Exception e)
            {
                message = e.Message;
            }
            return Content(message);
        }

I would like to display message when failed or success. this following line never execute

success: function (msg) {
                           alert("Success");
                        }

but my server side code is executed without any error.

Please provide your valuable suggestions,

Thanks,
Imdadhusen

Upvotes: 0

Views: 2177

Answers (4)

helvetesguten
helvetesguten

Reputation: 1

The obvious thing first: are you sure your url is valid? (I guess it is, since you state that the data is sent successfully).

Have you tried monitoring the ajax request to see what http status you recieve? Anyways, .ajax also has a error callback you can use in case of the request failing all together.

An apropos: where does the return from you function go? You have to send the reply to the output buffer for it to reach the client. Simply returning will send a null response to the client (but it shouldn't matter in this case since the .ajax success callback should trigger on request readyState 4 + status 200).

Upvotes: 0

Daveo
Daveo

Reputation: 19882

Instead of returning ActionResult you should be returning JsonResult

For example

public JsonResult SaveData() {
  return Json(new {success=true, msg="Saved ok"});
}

then your JS

success: function (msg) {
                            if(msg.success)
                               alert("Success");
                        }

Upvotes: 4

Jahan Zinedine
Jahan Zinedine

Reputation: 14874

Hit a breakpoint in Firebug on the send line of ajax script.

Hit a breakpoint in Firebug on the success method callback.

Hit a breakpoint in Visual Studio on the first line of action method.

Follow the steps to reproduce the scenario, and inspect the objects in Firebug before sending the request, continue and hit F8 to send the request, check the Net tab page in firebug and see if any request was sent or not.

If it was sent, inspect the posted request.

if until now everything is ok continue the process in the server side on VS and check if everything is right.

then check the response on the Firebug and see if nothing goes wrong.

Upvotes: 1

Ankur
Ankur

Reputation: 33657

You are returning Content result where as in JQuery you have mentioned dataType: 'json'. I would suggest you to either return JSON result from your controller or remove the dataType from jquery call. I am not sure about this, please give it a try.

Upvotes: 1

Related Questions