C# Web服务返回错误

3

我需要关于这个问题的帮助。请对我温柔一点,我不是专家 - 至少现在还不是。

问题是,我正在尝试通过JSON将数据从客户端(浏览器)发送到服务器(webservice)。 当我在Fiddler中查看POST数据时,我可以看到我发送的JSON。这是有效的(已测试)。 但是我的webservice(手动测试时返回OK),返回以下内容:“{" Message ":“处理请求时出错。”,“ StackTrace”:“”,“ ExceptionType ”:“”}”

不幸的是,它仅供内部使用,因此我无法为您发布webservice。

有人能向我解释出了什么问题吗?

1.) Javascript代码:

<script>
  var markers = "{param1:1, param2:\"test\"}";

    $.ajax({
        type: "POST",
        url: "http://xxx.xxx.xxx.xxx/site/util.asmx/CreateMarkers",
        // The key needs to match your method's input parameter (case-sensitive).
        data: JSON.stringify({ Markers: markers }),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) { alert(data); },
        failure: function (errMsg) {
            alert(errMsg);
        }
       });
</script>

2.) asmx代码

<%@ WebService Language="C#" Class="site.Util" %>
using System;
using System.Web.Services;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Services;

namespace site{

[WebService(Namespace="http://xxx.xxx.xxx.xxx/site/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

public class Util: WebService
{    
  [WebMethod]
  [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
  public string CreateMarkers(int param1, string param2)
  {
    return "OK";
  }
}

1
尝试不要将数据转换为字符串:data: markers, ... 看起来你的数据已经是匹配 param1、param2 参数的 JSON 格式了。你不需要一个根 Markers 属性。 - Andez
1
你也可以尝试使用 Postman 应用程序(https://www.getpostman.com/)来测试你的 Web 服务 API。它可以作为独立应用程序或浏览器插件使用。它允许你测试纯 API 来发送和接收数据,以确保你发送和接收的内容符合预期。 - jason.kaisersmith
@Andez 谢谢,但是如果没有你的修改,我在 Fiddler 中看到的是这样的:{"Markers":"{param1:1, param2:"test""} 我认为这部分是正确的,如果我按照你的建议进行更改,那么我得到的是 {param1:1, param2:"test"},这不是有效的 JSON。可能是我没有正确理解这个问题。你能再帮我一下吗? - Lars Hansen
2个回答

1
尝试格式化发送的数据以匹配Web服务方法。
<script>
    var markers = {param1:1, param2:"test"}; //<-- format the model

    $.ajax({
        type: "POST",
        url: "http://xxx.xxx.xxx.xxx/site/util.asmx/CreateMarkers",
        // The key needs to match your method's input parameter (case-sensitive).
        data: JSON.stringify(markers),  //<-- just send the markers
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) { alert(data); },
        failure: function (errMsg) {
            alert(errMsg);
        }
   });
</script>

1
在上面的答案中,标记被字符串化后再发送。在脚本代码中,var markers是一个JavaScript对象而不是JSON,因此在发送之前进行了字符串化。当使用上述建议时,在Fiddler中会看到什么? - Nkosi
这一定是了。你是第二个指出这种解决问题的方法的人。但是如果我不带一个根(标记)发送json,我该怎么办才能保证它仍然是有效的json呢? 我已经修改了asmx,使其在签名中只有一个类型字符串。 然后将脚本更改为var markers ="[ "Ford", "BMW", "Fiat"]"; 但还是出现相同的错误。 - Lars Hansen
然后我在Fiddler中看到了这个:["Ford", "BMW", "Fiat"]。来自Web服务的响应仍然是: {"Message":"处理请求时出错。","StackTrace":"","ExceptionType":""} - Lars Hansen
1
将问题改变成这样会让它变得混乱,更难以理解问题所在。请提供一个 [mcve] 以便重现该问题。我理解不想暴露内部代码的敏感性质,但如果没有清晰的问题描述,我怀疑您将无法得到适当的帮助。 - Nkosi
1
@LarsHansen 我建议在你的代码中设置断点,并调试出错的位置。 - Nkosi

0
我遇到过同样的问题,并通过在web.config中加入以下内容解决了它:
<customErrors mode="Off" />

在这个更改之后,Message、ExceptionType 和 Stack trace 被正确地填充,而不是在 mode=on 时对所有异常返回的标准“处理请求时发生错误”的信息,这对于调试或报告并不是很有帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接