weixin_33698043 2015-06-01 06:24 采纳率: 0%
浏览 45

如何构建AJAX请求

I need to call a WCF service from my webpage. The method in service uses generic list as an argument,i need to know how to frame the ajax query?

method:

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

1st time when method is called the 'fileList' is passed as null, this method populates the list and the send it back to the website. 2nd time the website sends the received fileList along with some status back to the service.

ajax query:

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 !");
        }
    });

 }

This works 1st time when jsonObj is null but doesn't works the second time when jsonObj contains value. It says:

Failed to load resource: the server responded with a status of 500 (Internal Server Error)

I think that the argument is not passed correctly during second time. Any idea? how to resolve.

  • 写回答

1条回答 默认 最新

  • weixin_33721427 2015-06-01 11:26
    关注

    finally found the answer, the data i'm sending to the service is wrong. 1st change

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

    value.d(or value.data in some case, check using debugger) contains the actual data.

    2nd change, Inside the ajax query write:

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

    instead of data: '{"status":"'+ status+'","fileList":'+jsonObj+' }',

    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀