How do I Pass Parameter in Ajax Url?
- by Nimit Joshi
I have developed a service which is running successfully. Following is my service code:
namespace WcfService1
{
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebInvoke(Method="GET", ResponseFormat = WebMessageFormat.Json, BodyStyle=WebMessageBodyStyle.Wrapped, UriTemplate="/display/{a}/{b}")]
string Display(string a, string b);
}
}
My Service:
namespace WcfService1
{
public class Service1 : IService1
{
public string Display(string a, string b)
{
int ab = Convert.ToInt32(a);
int bc = Convert.ToInt32(b);
int cb = ab + bc;
return cb.ToString();
}
}
}
How do i call this with the help of ajax url? I have tried out the following code but it is not working.
<script type="text/javascript">
$(document).ready(function () {
$('#BtnRegister').click(function () {
debugger;
var No1 = document.getElementById('TxtFirstNumber').value;
var No2 = document.getElementById('TxtSecondNumber').value;
$.ajax({
cache: false,
type: "GET",
async: false,
url: "http://localhost:22727/Service1.svc/Display",
data: 'a=' +No1+'&b='+No2,
contentType: "application/json; charset=ytf-8",
dataType: "json",
processData: true,
success: function (result) {
alert("data");
},
error: function (xhr, textStatus, errorThrown) { alert(textStatus + ':' + errorThrown); }
});
});
});
</script>