1

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. When I click on it I see the data, the data are printed but its indicating that I have a Uncaught SyntaxError:

Besa
  • 517
  • 1
  • 8
  • 19
  • try this at the end of your url replacing jsoncallback=? "?callback=jsonp_callback" – jarz Mar 25 '15 at 20:51
  • Same issue. I don't get it why is so – Besa Mar 25 '15 at 20:55
  • I think this might explain it: http://stackoverflow.com/questions/26262235/jsonp-returning-uncaught-syntaxerror-unexpected-token-angularjs-routingnu – jarz Mar 25 '15 at 20:59

1 Answers1

3

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",}]})

Checkout this plunker where I use your full json from a file with the wrapper:http://plnkr.co/edit/oX2UQRBA41FIHpwAP6AA?p=preview

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;
        }
    }
}
jarz
  • 732
  • 7
  • 20
  • The plunker does not open...still loading But where I should add that code that you wrote above ? – Besa Mar 25 '15 at 21:11
  • That goes on the json response. Do you have control over standings.json? Also, once the plunker loads, open the developer tools so you can see your console.log statement. – jarz Mar 25 '15 at 21:15
  • No I don't have control over standings.json. I retrieve the data from that site online [link](https://erikberg.com/mlb/standings.json) – Besa Mar 25 '15 at 21:17
  • Not on the response, sorry, on the json source, so on standings.json – jarz Mar 25 '15 at 21:17
  • So I need something eactly like this : http://jsfiddle.net/Rjfre/23/ but with my link – Besa Mar 25 '15 at 21:47
  • I cant access the api on this website https://erikberg.com/mlb/standings.json thats why I use jsonp. – Besa Mar 26 '15 at 14:31
  • Got it. I see what you're trying to do. I'm not sure you can request a regular json as jsonp without changing the actual json itself. What you can do is put a server in between your client and erikberg.com, get the json from that server, and expose it to your client, maybe as an endpoint. I have done this in the past with a nodejs server acting as the proxy server. Check out this SO response. http://stackoverflow.com/questions/24471288/how-can-i-use-ajax-to-retrieve-normal-json-as-jsonp – jarz Mar 26 '15 at 14:42
  • Hmm I am not that familiar with Proxy. Can I write an C# controller to get the data from the web api and manipulate it somehow. Is that possible ? – Besa Mar 26 '15 at 14:50
  • 1
    Yes, that's totally possible. Once you get it on your C# controller, you probably don't need to do anything to it. You can just return it as regular json. You do need to enable CORS on your web api controller, otherwise you'd face the same thing as going directly to erikberg.com. Something like this annotating your controller: [EnableCors("*", "*", "*")] – jarz Mar 26 '15 at 14:57
  • 1
    I added to the response a sample WebAPI controller you can use for this. Check it out. – jarz Mar 26 '15 at 15:12
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/73867/discussion-between-jarz-and-besa). – jarz Mar 26 '15 at 16:49