Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 传递的参数在WCF端为null_Jquery_Wcf_Json - Fatal编程技术网

Jquery 传递的参数在WCF端为null

Jquery 传递的参数在WCF端为null,jquery,wcf,json,Jquery,Wcf,Json,我正在使用Jquery在WCF端调用一个简单的方法 $.ajax({ type: "POST", url: "MyService.svc/TestJSON", data:'{"BikeId":"2"}', //data: '{"BikeId":"'+ id + '"}', contentType: "application/json; charset=utf-8",

我正在使用Jquery在WCF端调用一个简单的方法

$.ajax({
         type: "POST",
         url: "MyService.svc/TestJSON",
         data:'{"BikeId":"2"}',
         //data: '{"BikeId":"'+ id + '"}',                 
         contentType: "application/json; charset=utf-8",
         dataType: "json",
         success: function(msg) {
             alert(msg);
         },
         error: GetFailedMessage
     });
     function GetFailedMessage(msg) {
         alert('Error message. ' + msg.status + ' ' + msg.statusText);
     }
 });
我的简单TestJSON是[OperationContract]

public string TestJSON(string id)
{
    Bikes b = new Bikes();
    b.Description = "blah blah";
    b.Name = "DMX100";
    b.Make = "2010";
    b.ID = id;
    string bikeJson = JsonConvert.SerializeObject(b);
    return bikeJson;
}
我知道这个方法是通过使用断点调用的,但是参数“id”是空的。
这里缺少什么?

您的服务需要一个名为
id
的参数,并且在客户端,您使用
BikeId
作为参数名称发送值

TestJSON
方法签名中的参数名称:

public string TestJSON(string BikeId) {/*...*/}
或者修改客户端中的
数据
对象:

$.ajax({
  type: "POST",
  url: "MyService.svc/TestJSON",
  data: '{"id":"'+ id + '"}', // <------
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  success: function(msg) {
    alert(msg);
  },
  error: GetFailedMessage
});
$.ajax({
类型:“POST”,
url:“MyService.svc/TestJSON”,

数据:{“id”:“+id+”}”,您的服务需要一个名为
id
的参数,在客户端,您使用
BikeId
作为参数名发送值

TestJSON
方法签名中的参数名称:

public string TestJSON(string BikeId) {/*...*/}
或者修改客户端中的
数据
对象:

$.ajax({
  type: "POST",
  url: "MyService.svc/TestJSON",
  data: '{"id":"'+ id + '"}', // <------
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  success: function(msg) {
    alert(msg);
  },
  error: GetFailedMessage
});
$.ajax({
类型:“POST”,
url:“MyService.svc/TestJSON”,

数据:“{”id:“'+id+”}“,//我猜了一点,但当浏览器以“BikeId”的形式传递数据元素时,服务可能会将数据元素的名称推断为“id”?

我猜了一点,但当浏览器以“BikeId”的形式传递数据元素时,服务可能会将数据元素的名称推断为“id”?

考虑将代码格式化为代码在StAcExpBooT编辑器中。谢谢,我会记住这个。没问题,欢迎,如果你是新的,为什么你有比我更多的点?Grango-RAPP。显然是在我的就寝时间之前。考虑将你的代码格式化为StAcExcel编辑器中的代码。谢谢,我很新。所以请记住这一点。没问题,欢迎。如果你是新来的,为什么你的分数比我多?哈哈,很明显我已经过了就寝时间。