如何构建 AJAX 请求

How to frame AJAX request

我需要从我的网页调用 WCF 服务。服务中的方法使用通用列表作为参数,我需要知道如何构建 ajax 查询?

方法:

  public List<FileListEntity> ListLogFiles(string status, List<FileListEntity> fileList)
{
  .............//implementation....
  return fileList;
}

第一次调用方法时 'fileList' 作为 null 传递,此方法填充列表并将其发送回网站。网站第二次将收到的文件列表连同一些状态发送回服务。

ajax查询:

var status = defineStatus();
var value;
var jsonObj;
function callLogService(){
    debugger;

    if (value == null) {
        jsonObj = "null";
    }
    else {
       jsonObj=value;
    }
    $.ajax({
        type: "POST",
        url: "http://localhost:56256/SearchService.svc/ListLogFiles",
        data: '{"status":"'+ status+'","fileList":'+jsonObj+' }',
        crossDomain: true,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        processdata: true,
        success: function (response) {
            value = response;
            display(value);
            debugger;
            alert("Success");
            //debugger;
        },
        error: function () {
            alert("An error has Occured, Please try refreshing !");
        }
    });

 }

这在 jsonObj 为 null 时第一次有效,但在 jsonObj 包含值时第二次无效。 它说:

加载资源失败:服务器响应状态为 500(内部服务器错误)

我认为第二次参数传递不正确。 任何的想法?如何解决。

终于找到答案了,我发送给服务的数据是错误的。 第一次变化

if (value == null) {
    jsonObj = "null";
}
else {
   jsonObj=value.d;
}

value.d(或 value.data 在某些情况下,使用调试器检查)包含实际数据。

第二个变化, 在 ajax 查询中写入:

data: JSON.stringify({ status: status, fileList: jsonObj })

而不是data: '{"status":"'+ status+'","fileList":'+jsonObj+' }',