gpt4 book ai didi

javascript - 使用 jquery ajax 将字符串发送到 wcf 服务。为什么我只能发送数字字符串?

转载 作者:行者123 更新时间:2023-11-30 06:47:17 25 4
gpt4 key购买 nike

出于某种原因,在使用 jquery ajax 时,我只能将包含数字的字符串传递到我的 Web 服务。到目前为止,这还不是问题,因为我一直只是将 ID 传递给我的 wcf 服务。但我现在正在尝试做一些更复杂的事情,但我想不通。

在我的界面中:

    [OperationContract]
[WebInvoke(ResponseFormat = WebMessageFormat.Json)]
DataTableOutput GetDataTableOutput(string json);

我的网络服务:

public DataTableOutput GetDataTableOutput(string json)
{
DataTableOutput x = new DataTableOutput();

x.iTotalDisplayRecords = 9;
x.iTotalRecords = 50;
x.sColumns = "1";
x.sEcho = "1";
x.aaData = null;



return x;
}

Javascript/Jquery:

var x = "1";
$.ajax({
type: "POST",
async: false,
url: "Services/Service1.svc/GetDataTableOutput",
contentType: "application/json; charset=utf-8",
data: x,
dataType: "json",
success: function (msg) {
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
//alert(XMLHttpRequest.status);
//alert(XMLHttpRequest.responseText);
}
});

上面的代码完美运行。但是,当我将 x 更改为“t”甚至更改为“{'test':'test'}”时,我在 Firebug 中收到 Error 400 Bad Request 错误。

谢谢,约翰

编辑:取得一些进展!数据:JSON.stringify("{'test':'test'}"),将字符串发送到我的函数!

编辑2:

var jsonAOData = JSON.stringify(aoData);
$.ajax({
type: "POST",
async: false,
url: sSource,
contentType: "application/json; charset=utf-8",
data: "{'Input':" + jsonAOData + "}",
dataType: "json",
success: function (msg) {
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
//alert(XMLHttpRequest.status);
//alert(XMLHttpRequest.responseText);
}
});

enter image description here

EDIT3:我修改了放在上面 EDIT2 中的代码块。

交换 "和 ' 就成功了!

$.ajax({
type: "POST",
async: false,
url: sSource,
contentType: "application/json; charset=utf-8",
data: '{"Input":' + jsonAOData + '}',
dataType: "json",
success: function (msg) {
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
//alert(XMLHttpRequest.status);
//alert(XMLHttpRequest.responseText);
}
});

但是,我有一个新问题:

    public DataTableOutput GetDataTableOutput(DataTableInputOverview Input)
{

此处输入完全为空。我从 jsonAOData 传递的值没有分配给 DataTableInputOverview Input 变量。 :(

最佳答案

我修改了放在上面 EDIT2 中的代码块。

交换 "和 ' 就成功了!

$.ajax({
type: "POST",
async: false,
url: sSource,
contentType: "application/json; charset=utf-8",
data: '{"Input":' + jsonAOData + '}',
dataType: "json",
success: function (msg) {
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
//alert(XMLHttpRequest.status);
//alert(XMLHttpRequest.responseText);
}
});

这确实有效,但我必须修复发送到 GetDataTableOutputOverview 的对象的格式

关于javascript - 使用 jquery ajax 将字符串发送到 wcf 服务。为什么我只能发送数字字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5277761/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com