11

Morning,

I need to return a message from my web service. Below is a sample of my code, and i am returning a string.

[web method]
public string CheckFeedSubmission()
    {
        string responseText = "";
        try
        {
            //Stuff goes here
            responseText = "It Worked!"
        }
        catch (Exception ex) { responseText = "Opps wehave an error! Exception message:" + ex.Message; }
        return responseText ;
    }

I currently get the following response...

<string xmlns="http://tempuri.org/"/>

I would ideally like to return something like

 {"success" : true, "message" : "***Message Here***"}

I am sure once i get the idea of it, i will be able to return other items if needed. Its just this base i need to work out.

All help is much appreciated, thanks in advance :)

UPDATE: Just found this...

 return "{Message:'hello world'}"

Would i need something like

 responseText = "{"success" : true, "message" : \"There has been an error. Message: " + ex.Message + "\"}"
4

5 に答える 5

14

使用する:

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]//Specify return format.
public string CheckFeedSubmission()
    {
        string responseText = "";
        try
        {
            //Stuff goes here
            responseText = "It Worked!"
        }
        catch (Exception ex) { responseText = "Opps wehave an error! Exception message:" + ex.Message; }
        return responseText ;
    }

返される結果は次のようになります。

<string xmlns="http://tempuri.org/"/>
 {"success" : true, "message" : "***Message Here***"}
</string>
于 2012-07-12T08:10:39.413 に答える
2

Web メソッドの属性を使用してください

   [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]

呼び出し元は、コンテンツ タイプを application/json に設定して、webmethod を使用します。

于 2012-07-12T08:11:17.597 に答える
0

サービス応答で XML タグを削除するには、StackOverflow で次の回答を参照してください。

ASP.NET WebService が JSON 応答を XML タグでラップしています

于 2014-11-17T13:50:12.910 に答える