开发者

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);
                Upda开发者_运维问答teModel(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


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


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.


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.


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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜