4

所以我已经把头撞在墙上一天左右,试图让它发挥作用。如果它简单而愚蠢-对不起,谢谢。这是相当长的帖子,因为我试图描述到目前为止我所做的一切。

所以我有一个 ASMX Web 服务,我想用它来填充 Kendo UI 列表视图。在我将 data: 添加到我的传输请求之前,这非常有效。所以我的网络服务现在看起来像这样:

WebMethod(Description = "Return All Pending Actions Based")]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public PendingActionResult GetPendingActions(string sUsername, string sPassword, string sUserID, string sClubID)
{
   //code is here;
}

我的完整数据源如下所示:

dataSource = new kendo.data.DataSource({
                transport: {
                    read: {
                        type: "POST",
                        data:  "{'sUsername':'admin@mail.com','sPassword':'13123','sUserID':'1539','sClubID':'1'}",
                        url: "http://sdk.domain.com/services/general.asmx/GetPendingActions",
                        contentType: "application/json; charset=utf-8",
                        dataType: "json"
                    }
                },
                schema: {
                        data: "d.Data", // ASMX services return JSON in the following format { "d": <result> }. Specify how to get the result.
                        total: "d.Total",
                        model: {
                            id: "activityID",
                            fields: {
                                activityID: { type: "number", validation: {min: 1, required: true } },
                                taskName: { type: "string" },
                                taskNote: { type: "string" },
                                openDate: { type: "datetime" },
                                dueDate: { type: "datetime" },
                                priority: { type: "number" },
                                statusID: { type: "number" },
                                openedByID: { type: "number" },
                                assignedToID: { type: "number" },
                                statusName: { type: "string" },
                                complete: { type: "bool" }
                            }
                        }
                    }
            });

            that.set("pendingActionsDataSource", dataSource);

我回来的错误是:

{"消息":"无效 JSON 原语:"\u00261={\u00262=\u0027\u00263=s\u00264=U\u00265=s\u00266=e\u00267=r\u00268=n\u00269=a\u002610=m \u002611=e\u002612=\u0027\u002613=:\u002614=\u0027\u002615=a\u002616=d\u002617=m\u002618=i\u002619=n\u002620=@\u002621=m\u00226 \u002623=i\u002624=l\u002625=.\u002626=c\u002627=o\u002628=m\u002629=\u0027\u002630=,\u002631=\u0027\u002632=s\u002633=P\u0026 \u002635=s\u002636=s\u002637=w\u002638=o\u002639=r\u002640=d\u002641=\u0027\u002642=:\u002643=\u0027\u002644=1\u002645=3\u0026 \u002647=2\u002648=3\u002649=\u0027\u002650=,\u002651=\u0027\u002652=s\u002653=U\u002654=s\u002655=e\u002656=r\u002657=I\u0026 \u002659=\u0027\u002660=:\u002661=\u0027\u002662=1\u002663=5\u002664=3\u002665=9\u002666=\u0027\u002667=,\u002668=\u0027\u002669=s\u002670=C\u002671=l\u002672=u\u002673=b\u002674=I\u002675=D\u002676=\u0027\u002677=:\u00260278=\u0027=u 1\u002680=\u0027\u002681=}\u002682=".","StackTrace":"atSystem.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(Stringinput,Int32depthLimit,JavaScriptSerializerserializer)\r\natSystem.Web.Script.Serialization .JavaScriptSerializer.DeserializeT\r\natSystem.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContextcontext,WebServiceMethodDatamethodData)","ExceptionType":"System.ArgumentException"}JavaScriptSerializerserializer)\r\natSystem.Web.Script.Serialization.JavaScriptSerializer.DeserializeT\r\natSystem.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContextcontext,WebServiceMethodDatamethodData)","ExceptionType":"System.ArgumentException"}JavaScriptSerializerserializer)\r\natSystem.Web.Script.Serialization.JavaScriptSerializer.DeserializeT\r\natSystem.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContextcontext,WebServiceMethodDatamethodData)","ExceptionType":"System.ArgumentException"}

所以我开始搜索任何相似的东西,发现其他人在数据请求中缺少'或'所以我尝试了很多不同的变体,尝试使用 JSON.stringify 但错误仍在继续。所以我去了到提琴手看看什么被发送到服务器,这是我的问题。正在发送垃圾。提琴手在 TextView 中显示这个被发送到服务器:

0=%7B&1='&2=s&3=U&4=s&5=e&6=r&7=n&8=a&9=m&10=e&11='&12=%3A&13='&14=a&15=d&16=m&17=i&18=n&19=%40&20=m&21=a&22=i&23=l&24=.&25=c&26=o&27=m&28='&29=%2C&30='&31=s&32=P&33=a&34=s&35=s&36=w&37=o&38=r&39=d&40='&41=%3A&42='&43=1&44=3&45=1&46=2&47=3&48='&49=%2C&50='&51=s&52=U&53=s&54=e&55=r&56=I&57=D&58='&59=%3A&60='&61=1&62=5&63=3&64=9&65='&66=%2C&67='&68=s&69=C&70=l&71=u&72=b&73=I&74=D&75='&76=%3A&77='&78=1&79='&80=%7D

(我会在网上发布一张图片,这样更容易看到)

所以在这里我可以清楚地看到字符串没有以正确的格式发送。所以我决定试一试,不使用 Kendo 数据源,而只使用 JSON/AJAX。所以我输入了这个:

function getPAs() {
    $.ajax({
    type: "POST",
    url: "http://sdk.domain.com/services/general.asmx/GetPendingActions",
    data:  "{'sUsername':'admin@mail.com','sPassword':'13123','sUserID':'1539','sClubID':'1'}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: getPAs_Success,
    error: app.onError
    });
}

function getPAs_Success(data, status) {
    console.log("START getPAs_Success");

    var cars = data.d;
    var sout = document.getElementById('nav');
    var output = "";
    $.each(cars, function(index, car) {
    output += '<p><strong>' + car.taskName + ' ' +
                            car.taskNote + '</strong><br /> Priority: ' +
                            car.priority + '<br />Status: ' +
                            car.statusID + '<br />Opened By: ' +
                            car.openedByID + '<br />Assigned To' +
                            car.assignedToID + '</p>';
    });
    sout.innerHTML = output;
    console.log("END getPAs_Success");
}

如果我在 TextView 中查看提琴手,我会看到它被发送到服务器:

{'sUsername':'admin@mail.com','sPassword':'13123','sUserID':'1539','sClubID':'1'}

而且我也清楚地看到了我的 JSON 结果在提琴手中。

所以我的问题是:

为了实现这一点,我需要对 Kendo UI 数据源做些什么特别的事情吗?如果这很重要,我正在使用 Icenium 并尝试构建一个快速的移动应用程序来获得乐趣。

谢谢,

理查德

更新#1 两者都试过了,没有进一步的。

数据:{"sUsername":"admin@mail.com","sPassword":"13123","sUserID":"1539","sClubID":"1"},

它使用 jsonlint.com 进行验证,但是当我现在查看 fiddler 时,我看到它被发送到服务器:

sUsername=admin%40mail.com&sPassword=13123&sUserID=1539&sClubID=1

所以也许是因为我现在没有引用数据所以我尝试了这个:

数据:'{"sUsername":"admin@mail.com","sPassword":"13123","sUserID":"1539","sClubID":"1"}',

当我这样做时,我得到同样的 0=%7... 就像上面一样。

当我尝试使用 toJSON 时,我得到一个对象函数没有方法。做这样的事情:

var jsPost = $.toJSON({
                sUsername: "admin@mail.com",
                sPassword: "13123",
                sUserID: "1539",
                sClubID: "1"
            });

在 Telerik 论坛上发现有人说不要使用 toJSON 而是使用 JSON.stringify 所以我尝试了这个:

var jsPost = {
                sUsername: "admin@mail.com",
                sPassword: "13123",
                sUserID: "1539",
                sClubID: "1"
            };

...

data: JSON.stringify(jsPost),

但仍然导致疯狂的垃圾。

4

1 回答 1

0

有效的 JSON 格式需要双引号,而不是单引号。您可以尝试在http://jsonlint.com/等服务中验证您的字符串。更好的是,您可以在对象上使用toJson而不是手动构建它。

于 2013-08-09T18:24:45.287 回答