-1

I have built a REST web service in Java that sends out data in "application/json" format. It's running in a separate Linux machine on a different domain, and I am trying to access the REST data from my Windows machine, using jQuery $.ajax.

When I type in the URL in browser, it prompts me for a user-name password, as expected. On providing correct est of values, I could see the JSON output.

But when I try to do so by jQuery Ajax function, I keep getting the following error:

Uncaught SyntaxError: Unexpected token :

My server URL looks like this:

http://my.server.com:port/application/rest/1.0/Books

If I type that in a browser, I get the following text output in white background:

{ items: [{ BookId : 134675, LastReadDate: "2014-11-25 00:00:00.0", Source: null },{ BookId : 134676, . . . ]}

There is more data to it, but that's irrelevant to the problem at hand. Rest assured, the contentType is application/json and here's the AJAX function I am writing:

$(document).ready(function() { $.ajax({ url: "http://my.server.com:port/application/rest/1.0/Books",
type: "Get", crossDomain: true, success : function(data) { console.log(data); }, error: function(jqXHR, textStatus, errorThrown){ alert(textStatus + " " + errorThrown); }, dataType: "jsonp", contentType: "application/json", jsonp: false, jsonpCallback: "myMethod", username: "user1", password: "1234" }); });

Outside the jQuery AJAX, myMethod is defined as:

window.myMethod = function(data) { return data; }

The error method in jQuery AJAX is telling me that the jsonpCallback is not being called.

I found similar errors on stackoverflow, but none of the suggestions made it go away.

I use Chrome Inspector tools - there, the request is loading the JSON data as expected, but in the first line, where the items array is starting, the colon is probably not being accepted. It's expecting start of a callback, as I understood from other threads here. But no matter what I do, I cannot receive the callback({JSON}) format.

Please help. And yes, I have access to the server, and I tested the rest data with REST client extensions in both Chrome and Mozilla. The proper JSON object array is being returned.

Arijit Layek
  • 105
  • 2
  • 9

1 Answers1

0

I would advise you, first of all, to use a Notepad2 like editor to try to display weird characters in your source code.

If you use to copy and paste code from jsfiddle-like websites, they are a source of unknown (and not displayed) characters that bugs coders. Take a look at this post: Chrome Uncaught Syntax Error: Unexpected Token ILLEGAL

If this not gives a solution, hope it can try helping you debug it.

Community
  • 1
  • 1
rodrigogq
  • 1,943
  • 1
  • 16
  • 25