Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

211
Views
Getting parse error while fetching text file content in JQuery

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.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!