Johannes S Johannes S - 1 year ago 100
jQuery Question

Response from Ajax-Call not working

when I execute an ajax call, I get following error:

"Uncaught SyntaxError: Unexpected token :"

My jquery code looks like this:

type: 'GET',
url: "",
crossDomain: true,
dataType: "jsonp",
jsonp: "jsonp",
mimeType: "application/json",
jsonpCallback: 'callback',
contentType: "application/json; charset=utf-8",

beforeSend: function() {

success: function (data)
error: function (result) {


Why do I not get a json response. When using the same url in my browser directly, I get the expecting json response.
What is wrong?

Answer Source

You are surely experiencing a CORS issue.

This approach should perfectly work:


  // The 'type' property sets the HTTP method.
  // A value of 'PUT' or 'DELETE' will trigger a preflight request.
  type: 'GET',

  // The URL to make the request to.
  url: '',

  // The 'contentType' property sets the 'Content-Type' header.
  // The JQuery default for this property is
  // 'application/x-www-form-urlencoded; charset=UTF-8', which does not trigger
  // a preflight. If you set this value to anything other than
  // application/x-www-form-urlencoded, multipart/form-data, or text/plain,
  // you will trigger a preflight request.
  contentType: 'text/plain',

  xhrFields: {
    // The 'xhrFields' property sets additional fields on the XMLHttpRequest.
    // This can be used to set the 'withCredentials' property.
    // Set the value to 'true' if you'd like to pass cookies to the server.
    // If this is enabled, your server must respond with the header
    // 'Access-Control-Allow-Credentials: true'.
    withCredentials: false

  headers: {
    // Set any custom headers here.
    // If you set any non-simple headers, your server must include these
    // headers in the 'Access-Control-Allow-Headers' response header.

  success: function() {
    // Here's where you handle a successful response.

  error: function() {
    // Here's where you handle an error response.
    // Note that if the error was due to a CORS issue,
    // this function will still fire, but there won't be any additional
    // information about the error.

Source: Using CORS

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download