[英]How to get rid of Uncaught SyntaxError: Unexpected token :
I am having a controller like this : 我有这样的控制器:
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)
});
};
When I run it. 当我运行它。 I get Uncaught SyntaxError: Unexpected token : error. 我得到Uncaught SyntaxError:意外的令牌:错误。 When I click on it I see the data, the data are printed but its indicating that I have a Uncaught SyntaxError: 当我点击它时,我看到数据,数据被打印,但它表明我有一个Uncaught SyntaxError:
You need to wrap your json with the callback invocation. 您需要使用回调调用包装您的json。 Something like this. 像这样的东西。
angular.callbacks._0 (
{
"standings_date": "2014-09-29T00:00:00-04:00",
"standing": [
{
"rank": 1,
"won": 90,
"lost": 72,
"streak": "W1",}]})
Checkout this plunker where I use your full json from a file with the wrapper: http://plnkr.co/edit/oX2UQRBA41FIHpwAP6AA?p=preview 检查这个plunker,我从包含文件的文件中使用你的完整json: http ://plnkr.co/edit/oX2UQRBA41FIHpwAP6AA?p = preview
Here's a web api controller that does what you want. 这是一个web api控制器,可以满足您的需求。 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;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.