Returning JSON from ASMX, and handling it correctly in Javascript

后端 未结 5 1695
轮回少年
轮回少年 2020-12-18 02:07

I realize there are tonnes of similar questions already up here but I cannot figure this one out.

I have a Web Service (C#, .net 3.5). The essential Code you need

5条回答
  •  佛祖请我去吃肉
    2020-12-18 03:08

    Yes, definitely do not manually serialize the object. If you return a Member type, the framework will handle the JSON serialization for you.

    When you're seeing the [object XMLHttpRequest] alert, that sounds like it's getting into the error handler in your $.ajax() call, where the response passes in its XHR object as the first parameter. You're probably getting a 500 error on the server.

    Here's an example of decoding the ASP.NET AJAX error response in jQuery. Most simply, change your error handler to this:

    error: function(xhr, status, error) {
      var err = eval("(" + xhr.responseText + ")");
    
      alert(err.Message);
    }
    

    That will give you some insight into what the specific error is.

提交回复
热议问题