response not coming from webservice

Posted by Klbaiju under jQuery on 1/7/2013 | Points: 10 | Views : 1062 | Status : [Member] | Replies : 2
Hi,
In my application iam using jquery with webservice method for inserting to a table.
problem is records insertng correctly. but iam not getting response message from webservice.
this is the code
$(document).ready(function(){

$('#btnsave').click(function(){

var id=$('#txtid').val();
var fname=$('#txtfname').val();
var lname=$('#txtlname').val();
//alert(id);
//alert(fname);
$.ajax({
type: "POST",
url: "Dataprocess.asmx/insert",
data: "{'id': '" + id + "','fname': '"+fname+"','lname':'"+lname+"'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
if(msg=="Success")
{
$('#disp').css("color","green").html("Update successfully");
}
else
{
$('#disp').css("color","red").html("Error ckeck data");
}

},
error: function(xhr, status, error) {
alert('An error occurred: ' + xhr);
}

});

});

});
********************************************************
[WebMethod]
public string insert(string id, string fname, string lname)
{
SqlConnection con = new SqlConnection("Data Source=BAIJU-PC;Initial Catalog=Baiju;Integrated Security=True");
SqlCommand cmd = new SqlCommand("insert into emp values('" + id + "','" + fname + "','" + lname + "')", con);
string msg = "";
try
{
con.Open();
cmd.ExecuteNonQuery();
msg = "Success";
return msg;

}
catch (Exception ex)
{
msg = ex.Message;
return msg;

}
finally
{

con.Close();
cmd.Dispose();
}


}
how to solve this

Regards
Baiju




Responses

Posted by: Devi0074u on: 3/12/2013 [Member] Starter | Points: 25

Up
0
Down
I think you have to change the following things:

1. In the webservice, you have to uncomment this section.
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]

2. you need to change this code block (use msg.d instead of msg ).
success: function(msg) {
if(msg=="Success")
{
$('#disp').css("color","green").html("Update successfully");
}
}

Check for
if(msg != null && msg.d != null)
{
if(msg.d=="Success")
{
$('#disp').css("color","green").html("Update successfully");
}
}

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rimi1289 on: 6/13/2013 [Member] Starter | Points: 25

Up
0
Down
An article on how to return multiple values using JQuery Ajax and Asp.net web service.
Its a good article and worth reading, if you are still using ASMX

http://www.encodedna.com/2013/05/return-multiple-values-using-webservice.htm

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response