[英]calling a wcf 4.0 service from jquery($.ajax)
我在vs2010中開發了一個簡單的wcf服務
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetData(int value);
// TODO: Add your service operations here
}
public class Service1 : IService1
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
}
以下電話正在運作
protected void Button1_Click(object sender, EventArgs e)
{
ServiceReference1.Service1Client p = new ServiceReference1.Service1Client();
Label1.Text= p.GetData(5);
}
但是當我試圖從jquery調用它時它不起作用
$(".test").live("click", function () {
$.ajax({
type: "post",
url: "http://localhost/Service1.svc/GetData",
data: {value:'1'},
contentType: "application/json; charset=utf-8",
timeout: 10000,
processData: true,
dataType: "json",
success: function(d) {
alert(d);
},
error:function (xhr, ajaxOptions, thrownError){
alert(xhr.status);
alert(thrownError.toString());
}
});
任何人都可以幫助我,因為它給了我不眠之夜。 提前致謝。
先生,試試吧
$(".test").live("click", function () {
$.ajax({
type: "post",
url: "http://localhost/Service1.svc/GetData",
data: "{'value':1}",
contentType: "application/json; charset=utf-8",
timeout: 10000,
processData: true,
dataType: "json",
success: function(d) {
alert(d);
},
error:function (xhr, ajaxOptions, thrownError){
alert(xhr.status);
alert(thrownError.toString());
}
});
檢查通過線路傳遞的JSON的數據類型(使用Fiddler2或您選擇的瀏覽器的開發人員工具)。 我懷疑JSON將“1”作為字符串而不是整數傳遞。
看來問題出在您的數據參數中:
data: {value:'1'},
這不應該有單引號。 該服務正在嘗試解析具有字符串類型的“value”變量的方法。 改為:
data: {value:1},
這應該有助於服務解決正確的服務方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.