Add header in Jquery’s getJson function

Asked

Viewed 188 times

0

It is possible to add some header in jQuery’s $.getJson function?

I am having cross-origin problems and read in some places that adding some header information can solve this problem.

  • Cross Origin must be allowed on the server. Do you know if the endpoint you want to consume has this permission?

  • What is your problem specifically? some websites have requests blocked from outside, so there may be Cross Origin errors.

  • I think so, because only of the problem and one of the calls of getJson (this is the one that returns more data)

  • I try to capture data from http://spcultura.prefeitura.sp.gov.br/ but when I try to capture the agents of the cross-origin problem

  • You are trying to capture data in json?

  • Yes, capturing in JSON

Show 1 more comment

1 answer

2


$.getJSON is a simplified:

$.ajax({
    dataType: "json",
    url: url,
    data: data,
    success: success
});

Therefore, to set a header, a call like:

$.ajax({
    dataType: "json",
    beforeSend: function(request) {
        request.setRequestHeader("Authority", authorizationToken);
    },
    url: url,
    data: data,
    success: success
});

However, if the problem is CORS, the solution will only be if endpoint (server) allows CORS. See this reply to understand what he is and that one which contains an example of how to allow it. But if you don’t have the autonomy to change some configuration on the server, nothing can be done on the front end.

  • Using getJSON the request is run for about 2:30 minutes and returns error 504, using ajax with header request.setRequestHeader("Access-Control-Allow-Origin", "*"); the request is instantly refused and returns error 404. This may be indicative that the server that does not allow cross-origin?

  • 404 is not found. The request URL is correct? When not allowed, in the browser console it says the request was blocked by Cors.

  • The URL is correct, it’s the same one I used in getJSON, I even checked the error to see if it was the same and it was. It is also saying that it was blocked by CORS but changed the error code from 504 to 404.

  • 404 is not found, 504 is time out and 401 is unauthorized. Usually when the problem is only CORS, the status code is 401.

  • The amount of data to be searched is quite large, I had tried to limit the amount of data to be searched and so it worked. It has to increase the time limit?

  • There is the time out of Ajax, but it will serve only if it passes 15 minutes (that the default value it awaits). But if the site returns timeout after a certain time, back to the same case of Cors, has nothing to do :/

  • Thanks for the help :)

Show 2 more comments

Browser other questions tagged

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