KendoUI DataSource导致无效的JSON原语

时间:2013-08-09 16:59:36

标签: kendo-ui icenium

所以我一直在靠墙撞墙一天左右试图让它发挥作用。如果它简单而愚蠢 - 对不起,谢谢。这篇文章很长,因为我试图描述我到目前为止所做的一切。

所以我有一个ASMX Web服务,我想用它来填充Kendo UI列表视图。这完全有效,直到我添加数据:到我的传输请求。所以我的网络服务现在看起来像这样:

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);

我得到的错误是:

  

{ “消息”: “InvalidJSONprimitive:” \ u00261 = {\ u00262 = \ u0027 \ u00263 = S \ u00264 = U \ u00265 = S \ u00266 = E \ u00267 = R \ u00268 = N \ u00269 =一个\ u002610 =米\ u002611 = E \ u002612 = \ u0027 \ u002613 =:\ u002614 = \ u0027 \ u002615 =一个\ u002616 = d \ u002617 =米\ u002618 = I \ u002619 = N \ u002620 = @ \ u002621 =米\ u002622 = A \ u002623 = I \ u002624 = 1 \ u002625 = \ u002626 = C \ u002627 = O \ u002628 =米\ u002629 = \ u0027 \ u002630 =,\ u002631 = \ u0027 \ u002632 = S \ u002633 = P \ u002634 =一个\ u002635 = S \ u002636 = S \ u002637 = W \ u002638 = O \ u002639 = R \ u002640 = d \ u002641 = \ u0027 \ u002642 =:\ u002643 = \ u0027 \ u002644 = 1 \ u002645 = 3 \ u002646 = 1 \ u002647 = 2 \ u002648 = 3 \ u002649 = \ u0027 \ u002650 =,\ u002651 = \ u0027 \ u002652 = S \ u002653 = U \ u002654 = S \ u002655 = E \ u002656 = R \ u002657 = I \ u002658 = d \ u002659 = \ u0027 \ u002660 =:\ u002661 = \ u0027 \ u002662 = 1 \ u002663 = 5 \ u002664 = 3 \ u002665 = 9 \ u002666 = \ u0027 \ u002667 =,\ u002668 = \ u0027 \ u002669 =小号\ u002670 = C \ u002671 = 1 \ u002672 = U \ u002673 = b \ u002674 = I \ u002675 = d \ u002676 = \ u0027 \ u002677 =:\ u002678 = \ u0027 \ u002679 = 1 \ u002680 = \ u0027 \ u002681 =} \ u002682 = “堆栈跟踪 ””。“:” atSystem.Web.Script.Serialization.JavaScriptObjectDeser ializer.BasicDeserialize(Stringinput,Int32depthLimit,JavaScriptSerializerserializer)\ r \ natSystem.Web.Script.Serialization.JavaScriptSerializer.DeserializeT \ r \ natSystem.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContextcontext,WebServiceMethodDatamethodData) “ ”ExceptionType“:” System.ArgumentException“}

所以我开始搜索相似的任何东西的高低,发现其他人在数据请求中遗漏了'或',所以我尝试了大量不同的变体,尝试使用JSON.stringify,但错误仍在继续。所以我去小提琴手看看发送到服务器的是什么,这就是我的问题。垃圾邮件正在发送.Fiddler在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 dataSource就这样做,而只是使用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中查看fiddler,我会看到它被发送到服务器:

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

我清楚地看到我的JSON结果也是fiddler。

所以我的问题就是:

我需要使用Kendo UI Datasource做些什么特别的事情来解决这个问题吗?如果重要的话,我正在使用Icenium并尝试构建一个快速的移动应用程序以获得乐趣。

谢谢,

理查德

更新#1 试过这两个,没有进一步。

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

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

sUsername =管理员%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),

但仍然导致疯狂的垃圾。

1 个答案:

答案 0 :(得分:0)

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