2012-10-26 166 views
-1

我使用json返回數據的asp.net webservice,當我調用它時,它將數據返回給json,但將其嵌入到xml中。asp.net webservice返回JSON嵌入在XML ...?

我應該怎麼做在服務器端,以確保我的web服務僅返回JSON?

我的.asmx服務如下

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Services; 
using System.Web.Script.Services; 
using System.Web.Script.Serialization; 
using System.Text; 
using System.Collections; 
using System.IO; 
using System.Xml; 

[WebMethod(Description = "DemoMethod to get Total.")] 
public string GetTotal(string a, string b, string c) 
{ 
    List<Hashtable> objMyclass = new List<Hashtable>(); 
    JSonOutPutProperties jsonProperty = new JSonOutPutProperties(); 
    // 
    int total = Convert.ToInt32(a) + Convert.ToInt32(b) + Convert.ToInt32(c); 
    jsonProperty.Properties.Add("Total", total); 
    objMyclass.Add(jsonProperty.Properties); 
    // 
    JsonOutput objjson = new JsonOutput(); 
    objjson.objectcount = objMyclass.Count; 
    objjson.objectname = "Total"; 
    objjson.objectvalues = objMyclass; 
    // 
    JavaScriptSerializer js = new JavaScriptSerializer(); 
    string strJSON = js.Serialize(objjson); 
    return strJSON; 
} 
+0

你能否澄清你的問題。例如,網址的結尾是什麼;你使用的是什麼樣的服務(asmx,svc)。你可以發佈你的web服務中返回json的方法的一部分嗎? – surfmuggle

+0

你好threefouronesixonethree,發表更新的代碼,有任何想法是什麼問題 –

+0

高鹿你能解決這個問題,通過添加行'[ScriptMethod(UseHttpGet = false,ResponseFormat = ResponseFormat.Json)]'? 添加屬性後的結果是什麼? – surfmuggle

回答

0

如果添加下面一行到你的方法的問題可以得到解決:

[WebMethod(Description = "DemoMethod to get Total.")] 
[ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)] 
public string GetTotal(string a, string b, string c) 
{ 
    ... 

而且你可能要確保您正在使用post instead of get.。 Scott Guthrie有another good post on json

請在該帖子中查看以下問題how-to-let-an-asmx-file-output-jsonother links。我有a similar problem一次。

+0

我嘗試這個,但沒有效果仍然有相同的輸出... –

+0

你能提供更多的信息。 你調試過objjson,jsonProperty和objMyclass嗎? 誰在打電話給誰:在截圖中您打開瀏覽器,因此我認爲您可以通過在瀏覽器中輸入網址來訪問您的服務。如果您在另一個瀏覽器中打開它,它會是什麼樣子的;什麼是調用該方法的網址等。沒有更多的信息,這是在沒有釣魚竿的黑暗中釣魚。 – surfmuggle