2

我在 Breeze 1.1.2 的微风.debug.js 的第 12565 行收到无效字符错误。该行是:

var metadata = JSON.parse(data);

我研究了这条错误消息,我看到的最好的建议是使用未转义的单引号或双引号,或者无效字符。但是,我在回复中都找不到。

我正在使用 BreezeJS 发出请求并处理响应。Breeze.WebApi.EFContextProvider.Metadata() 正在生成响应。我在 Windows 8.1 上使用 IE11。生成响应的服务器是 IIS Express 8。我使用 Fiddler 捕获了请求和响应。

任何人都可以看到响应有什么问题吗?

要求:

GET http://localhost/RisingTide.API/breeze/scheduledpayments/Metadata HTTP/1.1
X-Requested-With: XMLHttpRequest
Accept: application/json, text/javascript, */*; q=0.01
Referer: http://localhost/RisingTide.Web/#/scheduledpayments
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; Touch; rv:11.0) like Gecko
Host: localhost
DNT: 1
Connection: Keep-Alive

回复:

HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Length: 862
Content-Type: application/json
Expires: -1
Server: Microsoft-IIS/8.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Sun, 15 Sep 2013 03:37:14 GMT

{"schema":{"namespace":"RisingTide.API.Models","alias":"Self","d4p1:UseStrongSpatialTypes":"false","xmlns:d4p1":"http://schemas.microsoft.com/ado/2009/02/edm/annotation","xmlns":"http://schemas.microsoft.com/ado/2009/11/edm","cSpaceOSpaceMapping":"[[\"RisingTide.API.Models.ScheduledPayment\",\"RisingTide.API.Models.ScheduledPayment\"]]","entityType":{"name":"ScheduledPayment","key":{"propertyRef":{"name":"Id"}},"property":[{"name":"Id","type":"Edm.Int32","nullable":"false","d4p1:StoreGeneratedPattern":"Identity"},{"name":"Description","type":"Edm.String","fixedLength":"false","maxLength":"4000","unicode":"true","nullable":"true"},{"name":"Amount","type":"Edm.Decimal","precision":"18","scale":"2","nullable":"false"}]},"entityContainer":{"name":"ScheduledPaymentsDbContext","entitySet":{"name":"ScheduledPayments","entityType":"Self.ScheduledPayment"}}}}
4

2 回答 2

0

看起来 JSON.parse 被\". JSON 似乎不支持这一点。

"[[\"RisingTide.API.Models.ScheduledPayment\",\"RisingTide.API.Models.ScheduledPayment\"]]"

引号必须是双反斜杠,如下所示:

"[[\\"RisingTide.API.Models.ScheduledPayment\\",\\"RisingTide.API.Models.ScheduledPayment\\"]]"

一定是\"在""里面被当做",但是\"在""里面被当做\"。

有趣的。直到我试过才知道。:-)


更新:这张 CDT 会话的截图应该有助于更好地解释发生了什么:

在 JSON 中使用引号需要双反斜杠

您可以看到,当您使用单个反斜杠时,JSON 将为 paring """,这不是有效的 JSON 字符串。但是使用 \,JSON 将解析一个 \ 和 3 "s,这是有效的。

于 2013-09-15T15:46:22.937 回答
0

Breeze v1.1.2 已经很老了。我们在早期版本中修复了许多类似的错误。当前最新的稳定版本是 1.4.2

于 2013-09-15T21:14:30.267 回答