繁体   English   中英

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

[英]Sending string to wcf service using jquery ajax. why can i only send strings of numbers?

出于某种原因,在使用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错误。

谢谢,约翰

编辑:取得一些进展! data: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);
                        }
                    });

在此处输入图片说明

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的对象的格式

暂无
暂无

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

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