如何从webservice返回JSON

时间:2022-10-18 14:35:43

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服务返回一条消息。下面是我的代码示例,我返回一个字符串。

[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 + "\"}"

5 个解决方案

#1


10  

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>

#2


2  

Please use the attribute for your webmethod

请使用您的webmethod属性。

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

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

调用者将其内容类型设置为application/json以使用webmethod

#3


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

#4


0  

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

要删除服务响应中的XML标记,请参阅*上的答案:

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

ASP。NET WebService正在用XML标记包装我的JSON响应

#5


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.

这是我对framewok 4.5.2的解决方案,在类FilterConfig中添加以下代码,注意:您将需要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());
    }
}

#1


10  

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>

#2


2  

Please use the attribute for your webmethod

请使用您的webmethod属性。

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

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

调用者将其内容类型设置为application/json以使用webmethod

#3


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

#4


0  

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

要删除服务响应中的XML标记,请参阅*上的答案:

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

ASP。NET WebService正在用XML标记包装我的JSON响应

#5


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.

这是我对framewok 4.5.2的解决方案,在类FilterConfig中添加以下代码,注意:您将需要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());
    }
}