How to show success message in JSON format after saving data using Web-service in C#?


I am trying to save data into database using web service[asmx], after saves successfully data into table I want to show successful message in JSON format 

Here is my code:

[WebMethod]

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

[ScriptMethod(UseHttpGet = true,ResponseFormat = ResponseFormat.Json)]


public bool rating(string email, string name,string age,string gender)

{

HttpResponse response = HttpContext.Current.Response;

System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();

string cs = System.Configuration.ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
int rowsInserted = 0;

bool t = false;

SqlConnection conn = new SqlConnection(cs);
conn.Open();

 

string sql = "insert into st_data values('" + email+ "','" + name+ "','"+ age+"','" + gender+ " ')";

SqlCommand cmd= new SqlCommand(sql, conn);
int i = cmd.ExecuteNonQuery();

if(i > 0)

// string strJSON = js.Serialize(t);

t = true;

return t;

}

I am Getting output message like this

<boolean xmlns="http://www.tezzfood.in/Apirestrating.asmx?op=rating/">true</boolean>

I want it to be in JSON format  like 

success[{true}]

 How can I achieve it?


Asked by:- RameshwarLate
0
: 6115 At:- 10/7/2017 9:15:39 AM
asmx c# web-service return-JSON-format-from-asmx







1 Answers
profileImage Answered by:- Sam

You can return JSON data from asmx using POST only

Make sure that the request is a POST request, not a GET. Scott Guthrie has a post explaining why.
Though it's written specifically for jQuery, this may also be useful to you:
Using jQuery to Consume ASP.NET JSON Web Services

Source

After that you can  change last line of code as

return  new JavaScriptSerializer().Serialize(YourObj);  

refer these links to learn about Web service in ASP.NET

  1. http://www.c-sharpcorner.com/UploadFile/8ef97c/web-service-in-Asp-Net-part-4/
  2. http://www.mikesknowledgebase.com/pages/Services/WebServices-Page2.htm
  3. http://williamsportwebdeveloper.com/cgi/wp/?p=494 (How to create JSON web service in asp.net)
1
At:- 10/7/2017 10:27:02 AM Updated at:- 10/7/2017 10:29:20 AM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use