【发布时间】: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