ASMX web service throws an exception got garbage in xhr.responseText on client
Got a standard AMSX web service with method which throws an exception:
[WebMethod(EnableSession = true)]
public JsonClientSideDataGrid LoadUserControl(string dataGridForm, string viewParameters, string dataGridSettings, bool isOnPopup)
{
// code is here
// ...
return result;
}
On a client side standard jQuery code:
jQuery开发者_如何学运维.ajax({
type: "POST",
url: webCallUrl,
processData: false,
data: jQuery.toJSON(data),
contentType: "application/json; charset=utf-8",
timeout: Configuration.WSTimeout,
dataType: "json",
success: callbackFunction,
error: function (XMLHttpRequest, textStatus, errorThrown) { errorFunction(XMLHttpRequest, textStatus, errorThrown, callId) }
});
The problem is: web service call works OK (I can see by debugger), but on client I always got a fail - error function is called. I assume that something bad happens during serialization of object to JSON. I could not diagnose that, VS does not stop on place of issue.
Moreover, textStatus contains always - "error", XMLHttpRequest.responseText contains some garbage.
What could be reasons for that? Any ideas how to diagnose the problem?
If you did not disable the help/test screen of your service, you should be able to test your method via web browser and see the serialization error there. It always worked well for me. The URL should be just simply:
http://<your_your_service_host_and_path>/<your_service_name>.asmx/LoadUserControl
Also, as suggested in the comment, you can try calling the web method as a standard method from code. In such case you should get the same error when you use the JavaScriptSerializer. I'm not 100% sure but this is probably the same that the ASMX services use. Quick example:
var result = new YourNamespace.YourService(...).YourMethod(...);
var serializedResult = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(result);
精彩评论