ASMX Web 服务抛出异常,客户端上的 xhr.responseText 中出现垃圾
得到了一个标准的 AMSX Web 服务,其方法抛出异常:
[WebMethod(EnableSession = true)]
public JsonClientSideDataGrid LoadUserControl(string dataGridForm, string viewParameters, string dataGridSettings, bool isOnPopup)
{
// code is here
// ...
return result;
}
在客户端标准 jQuery 代码:
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) }
});
问题是:Web 服务调用工作正常(我可以通过调试器看到),但在客户端上我总是遇到失败 - 错误函数是叫。我假设在将对象序列化为 JSON 期间会发生一些不好的事情。我无法诊断这一点,VS 不会在问题所在的地方停止。
此外,textStatus 始终包含 - “错误”,XMLHttpRequest.responseText 包含一些垃圾。
原因可能是什么?有什么想法如何诊断问题吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您没有禁用服务的帮助/测试屏幕,您应该能够通过网络浏览器测试您的方法并在那里看到序列化错误。它对我来说总是很有效。 URL 应该很简单:
另外,正如评论中所建议的,您可以尝试从代码中将 Web 方法作为标准方法调用。在这种情况下,当您使用 JavaScriptSerializer 时,您应该会遇到相同的错误。我不是 100% 确定,但这可能与 ASMX 服务使用的相同。快速示例:
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:
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: