简体   繁体   English

Ajax调用传递参数以起作用

[英]Ajax call pass parameters to function

What is wrong with this code? 此代码有什么问题? I am trying to pass parameters to a WCF function. 我正在尝试将参数传递给WCF函数。 I couldn't get this to work. 我无法使它正常工作。 I am getting Ajax error. 我收到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
    },
});

The data is not valid JSON. 数据无效的JSON。 What follows is the code and an example of its result. 以下是代码及其结果示例。

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

Instead of constructing JSON manually, I would use JSON.stringify and pass it an object. 我将使用JSON.stringify并将其传递给一个对象,而不是手动构造JSON。

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

I think you can see there is a difference. 我认为您可以看到有所不同。 Your code was missing commas and a quotation mark at the beginning of GId . 您的代码缺少逗号和GId开头的GId

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM