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 + "\"}"
thatuxguy
  • 2,418
  • 7
  • 30
  • 51
  • possible duplicate of [web service should return json](http://stackoverflow.com/questions/8205081/web-service-should-return-json) – Ascendant Feb 19 '14 at 23:47

5 Answers5

14

Use:

[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 ;
    }

The result returned will be like:

<string xmlns="http://tempuri.org/"/>
 {"success" : true, "message" : "***Message Here***"}
</string>
Luis
  • 5,786
  • 8
  • 43
  • 62
Ashwin Singh
  • 7,197
  • 4
  • 36
  • 55
2

Please use the attribute for your webmethod

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

The caller will have set his contenttype to application/json to use the webmethod

HatSoft
  • 11,077
  • 3
  • 28
  • 43
1

To remove the XML tags in your service response, see this answer on StackOverflow:

ASP.NET WebService is Wrapping my JSON response with XML tags

Community
  • 1
  • 1
kalenwatermeyer
  • 741
  • 12
  • 24
0

Try this one :

[WebMethod] 
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]//Specify return format. 
public bool addUser(UserModel um)
    {
        bool result = false;
        result = Conversion.intToBool(SplashAwardsDB.executeNonQuery(
            "INSERT INTO dbo.User ("
            + "userName, password, firstName, lastName, address, contactNo, birthDate, familyID, familyRole, x, y ) "
            + " VALUES ("
            + "'" + um.userName + "', "
            + "'" + um.password + "', "
            + "'" + um.firstName + "', "
            + "'" + um.lastName + "', "
            + "'" + um.address + "', "
            + "'" + um.contactNo + "', "
            + "'" + um.birthDate + "', "
            + "'" + um.familyID + "', "
            + "'" + um.familyRole + "', "
            + "'" + um.x + "', "
            + "'" + um.y + "')"
            ));
        return result;
    }
999k
  • 6,257
  • 2
  • 29
  • 32
0

This my solution for the framewok 4.5.2, In the class FilterConfig add the following code, Note: you will need the lib Newtonsoft.

 public class FilterConfig
{
    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Serialize;
        GlobalConfiguration.Configuration.Formatters.Remove(GlobalConfiguration.Configuration.Formatters.XmlFormatter);
        GlobalConfiguration.Configuration.EnableCors();
        filters.Add(new HandleErrorAttribute());
    }
}
Leonardo Mora
  • 349
  • 3
  • 11