【问题标题】:Ajax call pass parameters to functionAjax调用传递参数给函数
【发布时间】:2011-02-07 21:04:59
【问题描述】:

这段代码有什么问题?我正在尝试将参数传递给 WCF 函数。我无法让它工作。我收到 Ajax 错误。

  $.ajax({
    url: applicationPath + "/Test.svc/GetData",
    type: "POST",
    dataType: "json",
    data: '{GId":' + sender.get_value() + '"GName":' + sender.get_text() + '"mId":' + testId + '}',
    contentType: "application/json; charset=utf-8",
    success: function(result)
    {
        //trying to fill combobox here
    },
});

【问题讨论】:

    标签: javascript jquery asp.net ajax wcf


    【解决方案1】:

    数据不是有效的 JSON。以下是代码及其结果示例。

    '{GId":' + sender.get_value() + '"GName":' + sender.get_text() + '"mId":' + testId + '}'
    >> '{GId":'foo'"GName":'bar'"mId":5}'
    

    我不会手动构造 JSON,而是使用 JSON.stringify 并传递一个对象。

    JSON.stringify({GId: sender.get_value(), GName: sender.get_text(), mId: testid})
    >> "{"GId":"df","GName":"sdf","mId":4}"
    

    我认为您可以看到有区别。您的代码在 GId 的开头缺少逗号和引号。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-11
      • 1970-01-01
      • 2011-09-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多