c#webservice返回错误 [英] c# webservice return error
问题描述
我需要这方面的帮助.请对我温柔点,我还不是专家.
I need some help regarding this. Please be gentle with me, Im not an expert - yet.
问题是我试图通过 JSON 将数据从客户端(浏览器)发送到服务器(网络服务).当我在 Fiddler 中观看 POST 数据时,我可以看到我发送的 JSON.这是有效的(已测试).但是我的网络服务(当我手动测试然后我得到返回 OK)返回以下内容:{Message":处理请求时出错.",StackTrace":",ExceptionType":""}"
The thing is that Im trying to send data from a client (browser) to the server (webservice) via JSON. When I watch the POST data in Fiddler, I can see the JSON I send. This is valid (tested). But my webservice, (when I manually test then I get a return OK), returns the following: "{"Message":"There was an error processing the request.","StackTrace":"","ExceptionType":""}"
不幸的是,它仅供内部使用,所以我无法为您发布网络服务.
Unfortunately its only for internal use so I can't publish the webservice for you.
有人可以向我解释一下有什么问题吗?
Can someone explain to me whats wrong?
1.) Javascript 代码:
1.) Javascript code:
<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 代码
2.) asmx code
<%@ 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";
}
}
推荐答案
尝试格式化发送的数据以匹配 Web 服务方法
Try formatting the data sent to match the web service method
<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>
这篇关于c#webservice返回错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!