AJAX request with different domain

Asked

Viewed 278 times

0

my request

url="http://algumip:algumaporta/dowPDFNF?cChave="+chave

 $.ajax({
    url: url,
    dataType: 'jsonp',
    success: function (data) {
        console.log(JSON.stringify(data));
    },
    type: 'POST'
});

error that returns:

Uncaught SyntaxError: Unexpected token :

inserir a descrição da imagem aqui

However, when I pass the url (generated by the variable url) directly on the server, I get the right return. See image the return: inserir a descrição da imagem aqui

What I need is to get this return to have the pdf download

{"pdf":"http:\\\\algumip\\download\\6a423e04e2c2dfd4a59f0001ec8bbf44b9a6a6d-nfe.pdf"}
  • I believe that is not the case, because I can reach the other domain, problem is in reading the return "date" in Success

  • The problem should be this JSON.stringify, gives a console.log to see how the date is coming.

  • Places a Debugger inside the Success function. Success: Function (data) { Debugger console.log(JSON.stringify(data); } To see if you are logging into Success.

  • @Daniellearrudatorres Fact, change the title because in this case there is no relationship with CORS and the fact that the requirement is for another domain.

  • @Daniellearrudatorres you cannot do a jsonp post for another domain, the request cannot be get?

  • @Mayconf.Castro the Debugger returns me "Uncaught Syntaxerror: Unexpected Identifier"

  • @Leandroangelo I’ll switch to Get and test

  • Does not inform which callback.

  • The return treatment is also different, implement jsonpCallback to analyze the return.

  • Even with jsonp: "jsonpcallback", dataType: "jsonp". I still have the error return : Uncaught Syntaxerror: Unexpected token :

Show 6 more comments

1 answer

1


Try to add the item async: false, because according to the documentation, requests for jsonp remote servers do not work if the mode asynchronous is active.

Cross-Domain requests and dataType: "jsonp" requests do not support synchronous Operation.

$.ajax({ 
    async: false, 
    method: 'GET', 
    url: url, 
    contentType: "application/json", 
    jsonpCallback: minhafuncaocallback, 
    dataType: 'jsonp', 
    success: function (json) { 
        console.log(json); 
    }, 
    error: function (jqXHR, textStatus, errorThrown) { 
        console.log("AJAX ERRO" + jqXHR + textStatus + errorThrown); 
    } 
});

It is good that this request is made after the page has been fully loaded because a request synchronous prevents page loading from continuing until the response is received from that request which may decrease the quality of the user experience on your site, if applicable.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.