2017-02-03 110 views
2

我需要一些帮助。请温和我,我不是专家 - 但。c#webservice返回错误

的事情是,我试着通过JSON从客户端(浏览器)服务器(web服务)发送数据。 当我在Fiddler中观看POST数据时,我可以看到我发送的JSON。这是有效的(测试)。 但我的web服务,(当我手动测试,然后我得到一个返回OK),返回如下:“{”消息“:”有一个错误处理的请求“”堆栈跟踪“:””,‘ExceptionType’: “”}”

不幸的是,其只对内部使用,所以我不能发布web服务为您服务。

有人可以给我什么是错解释一下吗?

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

尝试匹配的web服务方法的数据,而不字符串化:数据:标记,...看起来您的数据已经是json格式,并且参数param1和param2的参数相匹配。你不想要一个根Markers属性。 – Andez

+1

您也可以尝试使用Postman(https://www.getpostman.com/)等应用来测试您的Web服务API。它可以单独使用或作为浏览器的插件。它允许你测试纯API来发送和接收数据,所以你可以确保你发送和接收的是预期的 –

+0

@Andez谢谢,但没有你的修改,这是我可以看到的小提琴手:{“标记” :“{param1:1,param2:\”test \“”}我假设这部分是正确的,如果我改变它,因为你建议然后我得到{param1:1,param2:“测试”}这是无效的JSON。这可能是我不明白这是正确的。你能进一步帮助我吗? –

回答

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

在上面的回答中,标记在发送前被串行化。在脚本代码中'var markers'是一个不是JSON的JavaScript对象,因此它在发送之前被字符串化。在使用上述建议时,你在小提琴手中看到了什么? – Nkosi

+0

这一定是它。你是第二个表明解决问题的方法。但我怎么发送一个没有根(标记)的json并且仍然有一个vailid json? 我修改了asmx,使它在签名中只有一个类型的字符串。 然后将脚本更改为var markers =“[\”Ford \“,\”BMW \“,\”Fiat \“]”; 但仍然是相同的错误 –

+0

然后,我看到这在提琴手:[“福特”,“宝马”,“菲亚特”]。来自webservice的回复仍然是:{“Message”:“处理请求时出错。”,“StackTrace”:“”,“ExceptionType”:“”} –