Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.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 WebGet-JSON复杂参数_Jquery_Asp.net_Json_Web Services_Webservice Client - Fatal编程技术网

Jquery WebGet-JSON复杂参数

Jquery WebGet-JSON复杂参数,jquery,asp.net,json,web-services,webservice-client,Jquery,Asp.net,Json,Web Services,Webservice Client,服务方法如下所示 public class UploadItem { public string fileName { get; set; } public string fileDesc { get; set; } } [OperationContract, WebGet] public int WriteUploadItem(UploadItem uploadItem) { //implementation } var data = { fileName: self.fil

服务方法如下所示

public class UploadItem
{
  public string fileName { get; set; }
  public string fileDesc { get; set; }
}
[OperationContract, WebGet]
public int WriteUploadItem(UploadItem uploadItem)
{
  //implementation
}
var data = {
  fileName: self.fileName(),
  fileDesc: self.fileDesc(),
};

$.ajax({
  url: "Fx.svc/WriteUploadItem",
  data: { uploadItem: data },
  success: function (result) {
    //implementation
  },
  error: function (result) { alert($.parseJSON(result.responseText).Message); }
});
调用如下所示

public class UploadItem
{
  public string fileName { get; set; }
  public string fileDesc { get; set; }
}
[OperationContract, WebGet]
public int WriteUploadItem(UploadItem uploadItem)
{
  //implementation
}
var data = {
  fileName: self.fileName(),
  fileDesc: self.fileDesc(),
};

$.ajax({
  url: "Fx.svc/WriteUploadItem",
  data: { uploadItem: data },
  success: function (result) {
    //implementation
  },
  error: function (result) { alert($.parseJSON(result.responseText).Message); }
});
这是由谁产生的

GET http://localhost:49701/Fx.svc/WriteUploadItem?uploadItem%5BfileName%5D
=2014-01-21.gif&uploadItem%5BfileDesc%5D=adesgfA HTTP/1.1
X-Requested-With: XMLHttpRequest
Accept: */*
Referer: http://localhost:49701/index.html#upload-queue
Accept-Language: en-AU,en;q=0.5
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko
Host: localhost:49701
DNT: 1
Connection: Keep-Alive
这些URL编码的参数在解码时如下所示

uploadItem[fileName]=2014-01-21.gif&uploadItem[fileDesc]=adesgfA 
已调用WebGet方法,但参数为null

我正在成功地将一个参数传递给另一个类似这样的方法

var userId = "73c2e254-5440-45eb-9099-58fa08dd037b"; // me.UserID();
$.ajax({
  url: "Fx.svc/UserFiles",
  data: { userId: userId },
  success: function (result) {
    //implementation
  },
  error: function (result) { alert($.parseJSON(result.responseText).Message); }
});
就我所见,唯一的区别是参数的值不同。最初有更多的字段,但我删除了更麻烦的数据类型,并很好奇地发现,即使是简单的字符串也会出现问题

怎么了?我该怎么办?是否需要在C#UploadItem类上添加某种属性?

美元.ajax()使用的默认编码是
application/x-www-form-urlencoded

要解决此问题,请控制编码并准确地告诉另一端使用了什么编码。注意对象的显式字符串化,以及
contentType
text/json
的显式编码

var data = {
  fileName: self.fileName(),
  fileDesc: self.fileDesc(),
};

$.ajax({
  url: "Fx.svc/WriteUploadItem",
  data: JSON.stringify(data),
  contentType: "text/json"
}).done(function (result) { 
  //implementation
}).error(function (err) { 
  //handle error
});
指定的内容类型编码也将用于返回值,除非指定其他编码。通常,返回JSON是非常令人满意的,因为$.ajax()会神奇地将其解析到结果对象中,但是当您返回一个简单类型(如
int
)时,您需要使用
dataType
参数指定text/plain的返回编码

$.ajax({
  url: "Fx.svc/WriteUploadItem",
  data: JSON.stringify(data),
  contentType: "text/json",
  dataType: "text/plain"
}).done(function (result) { 
  //implementation
}).error(function (err) { 
  //handle error
});