0

I'm trying to get response from the following cross domain ajax request, but get undefined in success:

$.ajax({
            type: "GET",
            url: "https://path_to_app/rest/jjwt",
            crossDomain: true,
            dataType: 'script',
            contentType: 'text/plain;charset=UTF-8',
            beforeSend: function(xhr) {
                xhr.setRequestHeader('Access-Control-Allow-Origin', '*');
                xhr.setRequestHeader("Authorization", "Basic " + btoa(username + ":" + password));
            },
            success: function (response, textStatus, xhr) {
                console.log(response);
                console.log(textStatus);
            },
            error: function (jqXHR, exception) {
                var msg = '';
                if (jqXHR.status === 0) {
                    msg = 'Not connect.\n Verify Network.';
                } else if (jqXHR.status == 404) {
                    msg = 'Requested page not found. [404]';
                } else if (jqXHR.status == 500) {
                    msg = 'Internal Server Error [500].';
                } else if (exception === 'parsererror') {
                    msg = 'Requested JSON parse failed.';
                } else if (exception === 'timeout') {
                    msg = 'Time out error.';
                } else if (exception === 'abort') {
                    msg = 'Ajax request aborted.';
                } else {
                    msg = 'Uncaught Error.\n' + jqXHR.responseText;
                }
                console.log(msg);
            }
        });

See the response in dev tools below: enter image description here

When trying to use the exact same url in browser, I can see the expected response (token) after authorization: server response

Oleksa O.
  • 827
  • 8
  • 16
  • 1
    CORS headers must be set by the **server**, not the client. – Pointy Sep 24 '19 at 12:33
  • 1
    `xhr.setRequestHeader('Access-Control-Allow-Origin', '*');` <-- useless in the client – epascarello Sep 24 '19 at 12:33
  • 1
    What about the error in your console which looks suspiously like it has something to do with your response? from the docs: `dataType "script": Evaluates the response as JavaScript and returns it as plain text` So your response gets evaluated and fails the `.` for an object property – empiric Sep 24 '19 at 12:34
  • Given your misuse of CORS, I would assume this question is a [duplicate of this](https://stackoverflow.com/q/20035101/519413) – Rory McCrossan Sep 24 '19 at 12:39
  • @empiric it looks, like that was it. Is there any safe workaround on that without changing the server's response dataType? – Oleksa O. Sep 24 '19 at 13:06
  • @Lefan I'm actually not sure what the best way to got would be if you can't change the dataType. What if you try and wrap the response in `"` on the server side? – empiric Sep 24 '19 at 13:31
  • if you expect to get an object from the request try to change `dataType: 'script',` to `dataType: 'json',` and `contentType: 'text/plain;charset=UTF-8',` in `contentType: 'text/json;charset=UTF-8',` in your ajax requeste code. For JSONP use `contentType: 'application/javascript;charset=UTF-8',` – Sim1-81 Sep 24 '19 at 15:21

0 Answers0