如何摆脱未捕获的SyntaxError的:意外的令牌: [英] How to get rid of Uncaught SyntaxError: Unexpected token :
本文介绍了如何摆脱未捕获的SyntaxError的:意外的令牌:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样一个控制器:
ListNewsCtrl.$inject = ['$http', '$scope', 'datacontext'];
function ListNewsCtrl( $http, $scope, datacontext) {
$scope.realTimeData = [];
var url ="https://erikberg.com/mlb/standings.jsonformat=json&jsoncallback=?";
$http.jsonp(url)
.success(function (data) {
$scope.realTimeData = data;
console.log($scope.realTimeData)
});
};
当我运行它。我得到未捕获的SyntaxError:意外的令牌:错误。当我点击它我看到的数据,该数据被打印,但它表明我有一个未捕获的语法错误:
When I run it. I get Uncaught SyntaxError: Unexpected token : error. When I click on it I see the data, the data are printed but its indicating that I have a Uncaught SyntaxError:
推荐答案
您需要回调调用来包装你的JSON。事情是这样的。
You need to wrap your json with the callback invocation. Something like this.
angular.callbacks._0 (
{
"standings_date": "2014-09-29T00:00:00-04:00",
"standing": [
{
"rank": 1,
"won": 90,
"lost": 72,
"streak": "W1",}]})
这结帐的地方plunker我使用完整的JSON从文件与包装: HTTP :?//plnkr.co/edit/oX2UQRBA41FIHpwAP6AA p = preVIEW
下面是一个网页API控制器,你想要做什么。周围的边缘有点粗糙,但它会让你更接近。
Here's a web api controller that does what you want. A little rough around the edges but it'll get you closer.
using System;
using System.Web.Http;
using System.Web.Http.Cors;
using RestSharp;
namespace Atom.Authorization.Controllers
{
[RoutePrefix("api")]
[EnableCors("*", "*", "*")]
public class StandingsController : ApiController
{
// GET api/standings
[Route("standings/")]
[HttpGet]
public String Get()
{
RestClient client = new RestClient("https://erikberg.com/");
RestRequest request = null;
request = new RestRequest("mlb/standings.json", Method.GET);
request.RequestFormat = DataFormat.Json;
IRestResponse response = client.Execute(request);
return response.Content;
}
}
}
这篇关于如何摆脱未捕获的SyntaxError的:意外的令牌:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文