Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

212
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda