Introduction
In this example i am calling the web sevice through ajax jquery.When click the button, the value from the text box taken as json and sent to the webservice through post method.The value stored in sql table(gopal).
|
Html- type this code in content editor web part
<script src="http://ajax.cdnjs.com/ajax/libs/json2/20110223/json2.js"/>
<script src="http://code.jquery.com/jquery-1.9.1.js" type="text/javascript"></script>
<script type="text/javascript">
function CallService()
{
jQuery.support.cors = true;
$.ajax({
type: "POST",
url:"http://localhost:16264/Service1.asmx/getdata1" ,
data:JSON.stringify({Name:$("#gope").val()}),
contentType: "application/json; charset=utf-8",
dataType: "json",
complete:comletesuc,
success: OnSuccess,
error: OnError
});
function comletesuc(data, status)
{ alert("Completed");
}
function OnSuccess(data, status,xhttp)
{ if(data.d="SUCCESS"){
alert("You account has successfully created with password 12345");}
else
{ alert("Not success");
}
}
function OnError(request, status, error)
{
alert("This is an error");
}
}
</script>
<div>Name:<input type="text" id="gope" /></div>
<div><input type="button" id="gope_sub" value="submit" onclick="return CallService();" /></div>
|
public string getdata1(string Name)
{
string status = "";
SqlConnection con = new SqlConnection("Data Source=dgfusgf;Initial Catalog=master;User
id=gopal;Password=password");
SqlCommand cmd = new SqlCommand();
con.Open();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "insert into gopal values('" + Name + "')";
int res = cmd.ExecuteNonQuery();
con.Close();
if (res == 1)
{
status = "SUCCESS";
}
else
{
status = "FAILURE";
}
return status;
}
|