【问题标题】:c# webservice return errorc# webservice返回错误
【发布时间】:2017-02-03 10:37:04
【问题描述】:

我需要一些帮助。请对我温柔一点,我还不是专家。

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

很遗憾,它仅供内部使用,因此我无法为您发布网络服务。

谁能给我解释一下怎么回事?

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";
  }
}

【问题讨论】:

  • 尝试不进行字符串化:数据:标记,...看起来您的数据已经是 json 格式,并且匹配了 param1、param2 的参数。您不想要根 Markers 属性。
  • 您也可以尝试使用 Postman (getpostman.com) 等应用程序来测试您的 Web 服务 API。它可以独立使用,也可以作为浏览器的插件使用。它允许您测试发送和接收数据的纯 API,因此您可以确定您发送和接收的内容符合预期
  • @Andez 谢谢,但如果没有你的修改,这就是我在提琴手中看到的: {"Markers":"{param1:1, param2:\"test\""} 我假设这部分是如果我按照您的建议进行更改,则正确,然后我得到 {param1:1, param2:"test"} 这是无效的 JSON。可能是我没有正确理解这一点。你能帮我进一步吗?

标签: c# json web-services


【解决方案1】:

尝试格式化发送的数据以匹配网络服务方法

<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>

【讨论】:

  • 在上面的回答中,标记在发送前被字符串化。在脚本代码中var markers 是一个 javascript 对象而不是 JSON,因此它在发送之前被字符串化。使用上述建议时,您在提琴手中看到了什么?
  • 一定是这个。你是第二个指出这种解决问题的方法。但是我如何发送一个没有根(标记)的json并且仍然有一个有效的json?我已经修改了 asmx,使它在签名中只有一个类型字符串。然后将脚本更改为 var markers ="[ \"Ford\", \"BMW\", \"Fiat\"]";但还是同样的错误
  • 然后我在提琴手看到这个:[“福特”,“宝马”,“菲亚特”]。来自网络服务的回复仍然是:{"Message":"处理请求时出错。","StackTrace":"","ExceptionType":""}
  • 像这样改变问题会使问题变得混乱并且更难以理解问题所在。提供可用于重现问题的minimal reproducible example。我理解不想公开内部代码但没有清楚地了解问题的敏感性质,我怀疑你会得到适当的帮助。
  • @LarsHansen 我建议在你的代码中放置断点并调试抛出错误的地方。
【解决方案2】:

我遇到了同样的问题,并在 web.config 中解决了这个问题:

<customErrors mode="Off" />

在该更改之后,Message、ExceptionType 和 Stack 跟踪会正确填充,而不是标准的“处理请求时出错”。当 mode=on 时,所有异常都会返回它,并且对于调试或报告不是很有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-10
    • 2017-12-20
    • 1970-01-01
    • 2020-10-31
    • 2011-04-26
    相关资源
    最近更新 更多