0

I am stuck on ajax error:

"Uncaught SyntaxError: Unexpected token :"

My Javascript is:

$(document).ready(function(e) {
$.get("http://agenda.myvnc.com:8895/datasnap/rest/TsrvServerMetodos/ag_get_convJson", null, function(result){
        $.each(result, function(i, field){
            $("div").append(field + " ");
        });
     },"jsonp");
});

The result from url is:

{"result":[[{"indice":1002,"nome":"ABET"},{"indice":75,"nome":"ALLIANZ"},{"indice":1013,"nome":"AMAFRERJ"},{"indice":127,"nome":"AMBEP"},{"indice":1012,"nome":"AMIL"},{"indice":185,"nome":"AMIL POR ADMINISTRA\u00C7\u00C3O"},{"indice":1007,"nome":"ASES"},{"indice":108,"nome":"ASSEFAZ"},{"indice":1015,"nome":"ASSIM"},{"indice":1025,"nome":"ASSIM PREFEITURA"},{"indice":197,"nome":"BNDES-FAPES"},{"indice":31,"nome":"BRADESCO (B)"},{"indice":148,"nome":"BRASIL  ASSIST\u00CANCIA"},{"indice":9,"nome":"CABERJ ( B)"},{"indice":141,"nome":"CARE PLUS"},{"indice":1014,"nome":"CASSI"},{"indice":194,"nome":"CAURJ"},{"indice":12,"nome":"CNEN ( R)"},{"indice":1016,"nome":"COLIGA\u00C7\u00C3O POLICIA"},{"indice":1003,"nome":"CONMEDH"},{"indice":140,"nome":"CORREIOS"},{"indice":1018,"nome":"DIX"},{"indice":93,"nome":"ELETRONUCLEAR ( R)"},{"indice":16,"nome":"EMBRATEL ( B)"},{"indice":1004,"nome":"FUNCEF"},{"indice":18,"nome":"FURNAS"},{"indice":1010,"nome":"GAMA ESCOLAR"},{"indice":207,"nome":"GAMA SAUDE"},{"indice":21,"nome":"GOLDEN CROSS ( B)"},{"indice":1005,"nome":"HOSPITAL DO ROCHA"},{"indice":129,"nome":"INB (P)"},{"indice":1006,"nome":"INTEGRAL S\u00C1UDE"},{"indice":1008,"nome":"INTERM\u00C9DICA"},{"indice":24,"nome":"IRB"},{"indice":213,"nome":"LIFE EMPRESARIAL"},{"indice":201,"nome":"MAYER SISTEMA DE SAUDE"},{"indice":149,"nome":"MEDIAL SA\u00DADE ( R)"},{"indice":25,"nome":"MEDISERVICE ( B)"},{"indice":74,"nome":"MUTUA ( R)"},{"indice":187,"nome":"N\u00DACLEOS"},{"indice":67,"nome":"NUCLEP (R)"},{"indice":26,"nome":"PARTICULAR"},{"indice":27,"nome":"PETROBRAS PETRO ( B)"},{"indice":28,"nome":"PETROBRAS\\ DISTR ( R)"},{"indice":103,"nome":"PORTO SEGURO ( R)"},{"indice":193,"nome":"REAL GRANDEZA"},{"indice":175,"nome":"SALUTAR SA\u00DADE"},{"indice":32,"nome":"SEGURO CANADA ( B)"},{"indice":1011,"nome":"SIND SA\u00DADE"},{"indice":122,"nome":"SOCIOS COTISTAS"},{"indice":35,"nome":"SUL AMERICA ( B)"},{"indice":1,"nome":"Tabelas Originais"},{"indice":69,"nome":"UNAFISCO"},{"indice":1017,"nome":"UNIBANCO\/TEMPO SAUDE"},{"indice":1022,"nome":"UNIMED (FABIO)"},{"indice":1021,"nome":"UNIMED (GUALBERTO)"},{"indice":1024,"nome":"UNIMED (MARCIO)"},{"indice":1020,"nome":"UNIMED (RAFAEL)"},{"indice":1019,"nome":"UNIMED (SCOFANO)"},{"indice":1023,"nome":"UNIMED (TATIANA)"},{"indice":1001,"nome":"UNIMED CL\u00CDNICA"},{"indice":44,"nome":"URMES URGENCIAS ( R)"},{"indice":135,"nome":"VISITANTES"}]]}

here is the fiddle code link: http://jsfiddle.net/QZ3ff/2254/

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Luiz Alves
  • 2,575
  • 4
  • 34
  • 75
  • That is not JSONP: probably the remote server does not support it. – lonesomeday Dec 30 '16 at 15:21
  • You have two issues. Firstly, the response is JSON, not JSONP so you should change the expected response type as JSON and JSONP are not interchangeable. Secondly, when you do that you get this error: `No 'Access-Control-Allow-Origin' header is present on the requested resource` This means that the third-party domain you're calling does not include CORS headers in the response, so cross-domain requests cannot be made to it through client side JS code. You will have to make the request from the server instead. See the question I marked as a duplicate for more information – Rory McCrossan Dec 30 '16 at 15:23
  • @RoryMcCrossan I'm gpoing to assume he can access the JSON just fine from his own server, just not from jsfiddle.net :) – Dale Dec 30 '16 at 15:24
  • if you validate your JSON response here http://jsonlint.com/, this throws an error on line 134: Error: Parse error on line 134: ...ce": 28, "nome": "PETROBRAS\ DISTR ( ----------------------^ Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', got 'undefined' – Jose Rojas Dec 30 '16 at 15:25
  • @Dale highly unlikely. The response has no CORS headers at all (wildcard or otherwise), and the code making the request makes no effort to identify the request originator. – Rory McCrossan Dec 30 '16 at 15:26
  • @RoryMcCrossan He told us his issue, it isn't with not getting the response – Dale Dec 30 '16 at 15:26
  • Yes it is. When blocked by the SOP you'll see the response in the network tab of the console absolutely fine, but the browser will be blocked from accessing that response programmatically. – Rory McCrossan Dec 30 '16 at 15:27
  • @RoryMcCrossan your crystal ball is obviously better than mine – Dale Dec 30 '16 at 15:28
  • You can see that from this fixed jsFiddle (where the loop is through `result.result` instead of `result`, which is OPs code issue): http://jsfiddle.net/QZ3ff/2254/ – Rory McCrossan Dec 30 '16 at 15:28
  • No it isn't. The OP has posted the response they got. The JSON is invalid. – phuzi Dec 30 '16 at 15:28
  • No, I just tested and the json is valid. I can access the url and get the json using java in android studio without problem. – Luiz Alves Dec 30 '16 at 16:07
  • @Rory McCrossan Many thanks, I will go enable CORS headers in my server. Another question, can I use "post" with ajax and CORS? – Luiz Alves Dec 30 '16 at 16:38
  • Glad to help, and yes you can – Rory McCrossan Dec 30 '16 at 16:39
  • @Rory McCrossan Thanks again, Now it´s working. Now the problem is with parse json response. I get Uncaught SyntaxError: Unexpected token o in JSON at position 1 at JSON.parse (). Please see at http://jsfiddle.net/QZ3ff/2267/ – Luiz Alves Dec 30 '16 at 17:14
  • That's because the response is already deserialized for you by jQuery - doing it again is causing the error. Try this: http://jsfiddle.net/QZ3ff/2268/. Note that the `Unexpected token <` error is being thrown by the google font that you've included in the external resources. – Rory McCrossan Dec 30 '16 at 17:21
  • @Rory McCrossan Very Good. Many thanks and happy new year. – Luiz Alves Dec 30 '16 at 17:27
  • Thanks, and to you! – Rory McCrossan Dec 30 '16 at 17:32

0 Answers0