【问题标题】:How to frame AJAX request如何构建 AJAX 请求
【发布时间】:2015-06-01 06:24:49
【问题描述】:

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

方法:

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

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

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 为空时第一次有效,但在 jsonObj 包含值时第二次无效。 它说:

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

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

【问题讨论】:

    标签: jquery ajax wcf rest restful-url


    【解决方案1】:

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

    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+' }',

    【讨论】:

      猜你喜欢
      • 2015-06-28
      • 1970-01-01
      • 2020-08-11
      • 2016-11-05
      • 1970-01-01
      • 1970-01-01
      • 2015-11-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多