我需要一些幫助。請溫和我,我不是專家 - 但。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";
}
}
嘗試匹配的web服務方法的數據,而不字符串化:數據:標記,...看起來您的數據已經是json格式,並且參數param1和param2的參數相匹配。你不想要一個根Markers屬性。 – Andez
您也可以嘗試使用Postman(https://www.getpostman.com/)等應用來測試您的Web服務API。它可以單獨使用或作爲瀏覽器的插件。它允許你測試純API來發送和接收數據,所以你可以確保你發送和接收的是預期的 –
@Andez謝謝,但沒有你的修改,這是我可以看到的小提琴手:{「標記」 :「{param1:1,param2:\」test \「」}我假設這部分是正確的,如果我改變它,因爲你建議然後我得到{param1:1,param2:「測試」}這是無效的JSON。這可能是我不明白這是正確的。你能進一步幫助我嗎? –