0

I have an API website I made and I want my javascript to read in the values. I have a page http://ec2-54-152-248-65.compute-1.amazonaws.com/temp/ that has this on it:

{'records': [{'a': 'a'}]}

and I want my javascript to read it in and convert it to a JSON. I have this code so far and it isn't working:

$http({
    method: 'JSONP',
    url: "http://ec2-54-152-248-65.compute-1.amazonaws.com/temp/"
}).success(function(response) {$scope.names =      JSON.parse(JSONize(response.records));});

 });

Does anyone have any theories on how I could convert this to a JSON and have it read into my web app as something I can ng-repeat.

This is the error in my console.log:

(index):1 Uncaught SyntaxError: Unexpected token :
7
  • 1
    i mean to show what the server response to you Commented Nov 8, 2015 at 4:48
  • 2
    @ThomasP1988 you can see the server response using a curl, the URL is working ;) Commented Nov 8, 2015 at 4:50
  • 1
    ah yes you are right, but i still would like to see what he gets from his request because i had problems in the past with unwrapped json and jsonp request Commented Nov 8, 2015 at 4:52
  • 1
    ok, change the method from 'jsonp' to 'get' and change '.success' by '.then' Commented Nov 8, 2015 at 5:15
  • 1
    Looks like, you should write $scope.names = JSON.parse(response).records; Commented Nov 8, 2015 at 5:39

3 Answers 3

1

Your response looks like a wrong JSON response, used single quote instead of double quote, so a quick solution can be:

$scope.names = JSON.parse(response.replace(/'/g, '"'));

Or fixing your server-side code to generate correct JSON response.

Sign up to request clarification or add additional context in comments.

12 Comments

I corrected it to have double quotes, but it still gives the same error Uncaught SyntaxError: Unexpected token :
@Rob I don't know what is the JSONize function you're using, but I think you don't need that, just do JSON.parse(response).
OK, I see an unexpected ; after the url: "..."! Remove that please.
I don't see any code supposed to show something in console, show us how changing $scope.names will effect the console, then we can help you more. You can add a line console.log($scope.names) after the line $scope.names = ... to see the result of JSON.parse
@Rob Dear friends, you need to know exactly what you want to do with the response, I see you're not parsing response at all. So if you want me to be able to help you explain what are you doing there! And thank me with a +1 if you think it helped you ;)
|
0

By looking at your code

$http({
    method: 'JSONP',
    url: "http://ec2-54-152-248-65.compute-1.amazonaws.com/temp/"
    }).success(function(response) {
       $scope.names =  JSON.parse(JSONize(response.records));
    });
});

you should

$scope.names =  JSON.parse(JSONize(response)).records;

and if you don't want to change your server side code, do what @MostafaR recommended or use eval function. (for the problem caused because of used single quote instead of double quote)

$scope.names =  eval( '(' + response + ')' ).records;

Comments

0

You are mixing JSON and JSONP. You have a JSON object, but you're calling it with JSONP.

To avoid the CROS origin policy, you may need to use JSONP. To call your object with JSONP, you need to ensure the object is 'callable' via JSONP, and that means you need it to have the format:

 mycallback({ foo: 'bar' });

See here for a good explanation: What is JSONP all about

That is what CROS policy is all about: ensuring you access the data from another origin (domain) because you have the right to do so, because you somehow know what function the object is encapsulated in.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.