last day (15 days later) » 

8:31 PM
3
A: How to process properly JSONP in AngularJS?

milanlemperaThis is not an AngularJS problem. Uncaught SyntaxError: Unexpected token is javascript syntactic error - in devtools the right of error message, you have link to bad line in source code. If your code is exactly the same as in question, problem is this line var data_obj = {username: "aaaaa", pa...

 
the code is not exactly the same (the brackets where a typing error here. I just corrected it). Please check my updated question how do I check server's response? If I click the file (login.json?callback=angular.callbacks._0:1 Uncaught SyntaxError: Unexpected token in my case) corresponding to the error it shows something like you said but without angular_callbacks._0 wrapping. This was my actual question.
 
Your API return probably only json, for JSONP must be JSON wrapped with callback. Look at server response in devtools. If it is true, you can use $http.get instead of jsonp
If response have JSONP format codeangular.callbacks._0 ({"here": "is", "valid": "json"})code then json in function is probably invalid.
 
@milanlempera, I didn't get your last comment. Also, please send my second edit
see* (mispelled)
 
If you open devtools in Network tab, you see all requests. Select request due to $http.jsonp and switch to response tab If api support JSONP response contents angular.callbacks._0 ({"here": "is", "valid": "json"}), therefore valid json with function call padding (see en.wikipedia.org/wiki/JSONP). Error message can mean that json in function is not valid (and containd bad : character). If response contents is only json {"here": "is", "valid": "json"}, API does not support JSON and you should use $http.get function, but with EDIT 2 this is irelevant
 
thank you very much for your further comment. what is irrelevant? Please see the result in Edit 3
hey milanlempera SO suggested me to move the conversation to a chat. I hope this is fine for you. and thank you again for your help
 
8:42 PM
Are urls generetad by jquery and angular same (except callback parametr value)?
 
no, this is a default URL
 
but this is API problém...
 
what do you mean exactly?
 
i think, if api support JSONP and request content callback, response should be wraped by callback
I noticed one more error, in $http.jsonp first argumen is url, second is config object
 
ok, let's suppose it doesn't support JSONP. $.get violates CRUD requests. But with $.ajax (which is the original code) there is no problem at all
 
8:49 PM
in your example you pass in second param url parameters, but corectly is { params: {url parameters here}
is possible that the API without parameters behaves differently?
because username and password are not included in the url
 
I corrected the syntax mistake. thank you. However the error I get is the same (just the link showed includes the parameters in GET)
The service is configured to use default parameters
Let me make a quick try by passing the full list of expected parameters
I just tried to pass the exact expected parameters (using .jsonp) but the error (unexpected :) doesn't change.
I'm sure the response depends on the parameters because one of the parameters in the response changed when I change one in the request
 
one more irregularity - in jquery example you have used type: 'POST' but i think JSONP can make GET requests only...
i try run this code in jquery:
 
that's not my code :-) I'm just converting it to an AngularJS application. Yes, I'm aware that POST doesn't make sense with JSONP and actually in the official documentation it's stated that I MUST use POST which would explain why the response is not jsonp
 
and result is Uncaught SyntaxError: Unexpected token : ;-)
 
ok, one moment please
 
9:02 PM
I must go, but my summary is, if API support JSONP and url contain callback parametr, api must return json result wrapped in callback function.
CORS (en.wikipedia.org/wiki/Cross-origin_resource_sharing) is diferent problem, its solution can be JSONP, but only if API supports it
other solutions can be Access-Control-Allow-Origin headers
 
apparently that's not even the function called (the breakpoint is not triggered).
However thank you very much.
I'll accept your answer for the effort :-)
 
Not at all
 

last day (15 days later) »