2017-02-03 174 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」:「」} –