Sunday 15 September 2013

javascript - How to handle response of a POST request in jQuery -


I am trying to post some data to my ASP.NET MVC Web API controller and bring it back in the feedback I am trying. I have the following script for this post:

  $ ('# recordUser'). Click (function () {$ .ajax ({type: 'POST', url: 'api / recordUser', data: $ ("# user records user"). Serialize (), Data type: 'json', success : Function (useremail) {console.log (useremail);}, error: function (xhr, status, err) {}, complete: function (xhr, position) {if (condition === 'error' ||! Xhr .responseText) {Warning ("Error");} Other {var data = xhr.responseText; Alert (data); / / ...}}})}};   

The problem with this script is that whenever I try to post data, jQuery comes back to "error" instead of "success."

I have made sure that there is no problem with my controller whenever I request it, so I can get it in my API method in debug mode and see that it is getting data from POST request The controller is quite simple:

  public class recorder controller: API controller {public recordUser e-maildito post (RECUSS eMDDTO user e-mail) {return User eMail; }}   

I'm not sure how I can get jQuery to print any useful error message. Currently when I try to debug the jQuery code using Chrome console, it shows an empty xhr.responseText, there is nothing in the "Error" object and set the "status" to "Error" That's what you are looking for is not quite helpful.

Whatever I have tried, it is to run the following code directly from another console:

  $ Ajax ({type: 'POST', url: 'api / recordUser', data: {"email": "email@address.com"}, datatype: 'jason', success: function (useremail) {console.log ( Useremail);}, error: function (xhr, status, err) {console.log (Xhr); console.log (err); console.log (status); warning (err.Message);}, complete: work ( Xhr, position) {if (position === 'error' ||! Xhr reaction text) {warning ("error");} and {var data = xhr.responseText; warning (data);}}});   

i.e. Actually using the same script without clicking on the button and submitting a form is surprising, it comes back with the right response and I can see my data printed in the console. For me, this means that at least my web API controller is working fine but I have no indication that it is not working on clicking buttons or submitting the form and "error" instead of "success" "Goes into.

I have failed to find an error in my view and I would be happy if a person can help me respond to the form.

No comments:

Post a Comment