0

I tested my php alone and it gave me this result

{"tabId":1,"tabName":"Main","uId":"1"}{"tabId":2,"tabName":"Photography","uId":"1"}

but my angularjs can't receive the callback, it return an error somewhere in angularjs

userId = '1';

$http({
    url: "php/loadTab.php",
    method: "GET",
    params: {'userId':userId}
    }).success(function(data, status, headers, config) {
        console.log(data);
    }).error(function(data, status, headers, config) {
    });

the wierd thing I reuse the exact ajax code and it worked previously. Any thought on this?

the error : SyntaxError: Unexpected token { at Object.parse (native)

the more strange thing : I purposely add another echo on my php and it able to console the value. what?!

4
  • This code by itself seems correct so it's probably an issue with your web service. Commented Apr 22, 2014 at 15:49
  • what is the error that it returns? Commented Apr 22, 2014 at 15:50
  • @adrichman SyntaxError: Unexpected token { at Object.parse (native) Commented Apr 22, 2014 at 15:54
  • your response data needs to be contained in one object Commented Apr 22, 2014 at 15:55

1 Answer 1

1

The data from the server isn't valid JSON.

$httpProvider.defaults.transformResponse will try to parse the JSON if the data looks like JSON.

https://docs.angularjs.org/api/ng/service/$http

To fix this you could make the parent an array of objects like this

[{"tabId":1,"tabName":"Main","uId":"1"},{"tabId":2,"tabName":"Photography","uId":"1"}]
Sign up to request clarification or add additional context in comments.

2 Comments

good finding! I don't want it to be JSON, I want it to be normal js objects, how?
JSON = JS objects, the problem here is that multiple JS objects should be inside array brackets []. Rather than trying to reconfigure angular's $http parser, I would suggest using PHP's json_encode

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.