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?
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
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly