0

I am trying to fetch data from text file which resides on server. I have access of that location and able to see content when I put URL in browser tab. I am trying to make AJAX call and get file content, but I am getting Error: Uncaught SyntaxError: Unexpected identifier

Code

    function logResults(json){
  console.log(json);
}

$.ajax({
  url: u,
  dataType: "jsonp",
  jsonpCallback: "logResults"
});

enter image description here

on console, enter image description here

I tried below code too, but same result,

     $.ajax({
   type: 'GET',
   url: u,
   crossDomain: true,
   dataType: 'jsonp',
   async: false,
   headers: {
     "Access-Control-Allow-Origin": "*"
     },
   success: function(succ) {
     console.log("Success: ", succ)
   },
   error: function(err) {
     console.log("Error: ", err)
   }
 });

This code is always going into error block.

1
  • 1
    does the server actually return jsonp - or are you desperately trying all the usual "tricks" to circumvent CORS issues ... CORS is controlled by the server ... adding "Access-Control-Allow-Origin": "*" shows that you don't know that Commented Apr 27, 2022 at 10:57

2 Answers 2

1

You said:

dataType: "jsonp",

But the URL is responding with:

Deployment automatically finished…

Which is not JSONP, it is plain text.

You get the error when it tries to execute the plain text as JSONP.

Don't put wrong information in the dataType field.


async: false,

That's deprecated. Don't use it. It's also pointless since you are using callbacks.

It's also incompatible with JSONP so it is ignored (until you remove the incorrect dataType).


crossDomain: true,

This has no effect unless you are making a:

  • non-JSONP request
  • to the same origin
  • which gets redirected to a different origin

… which is very rare.


headers: {
    "Access-Control-Allow-Origin": "*"
 },

Access-Control-Allow-Origin is a response header. It doesn't belong the request. Trying to add it to the request will cause extra problems as it will make the request preflighted.

(At least, that would be the case if you hadn't said dataType: 'jsonp' because adding request headers is incompatible with JSONP).


All you need on the client

The only code you need on the client is:

function logResults(result){
   console.log(result);
}

$.ajax({
    url: u,
    success: logResults
});

The server you are requesting the data from will need to use CORS to grant you permission to access it if it is a cross-origin request.

Sign up to request clarification or add additional context in comments.

Comments

0

It is because you have added dataType as jsonp, so that it will try to parse the response to JSON and if the response is not a JSON it will throw error.

5 Comments

it will try to parse the response to JSON -actually it won't ... it will load it as a <script> - but other than that everything up to the , is correct
it will fail - since JSON isn't valid javascript (though, if the data is an array, it may not throw an error since it's possible to have a statement in javascript like []
Understood bro, it expects a script right
it actually expects something like function xyz() { return something_that_is_valid_in_javascript } and the underlying code will call result = xyz() ... that's when the error would happen if the JSON response was parsable as javascript, since there was no function
It expects name_of_callback_function_passed_in_url({some: "data", that: "is usually an object literal", here: true }); and jQuery (or whatever JSONP library) will have created window.name_of_callback_function_passed_in_url when it made the request. In the OP's version, they are overriding the auto function generation with logResults which isn't good for handling parallel requests.

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.