0

Hello I have an ajax request, which gets data as json as callback. The content-type is not displayed in the response header and i get null when i check in the console. My code:

$.ajaxSetup({
    cache: false
    beforeSend: function (jqXHR) {
        ...
    },
    complete: function (jqXHR) {
        ....
    }
});

function loadData(div, url) {
    $('#' + div).jstree({
        core: {
            data: function (node, cb) {
                $.getJSON(url + node.id)
                    .done(function (data, status, xhr) {                        
                        cb.call(this, data);
               .......

The response looks like:

Response

And i get XML processing error: syntax error

I tried adding 'Content-Type': 'appplication/json' to the $.ajaxSetup but there was no change. How can i handle this error. Should it be in the ajaxsetup or in the $.getJSON function.

  • i don't understand, are you tring to get an XML response by getJson? if so this may helps you https://stackoverflow.com/questions/8909668/how-to-get-json-from-an-xml-response-from-server – Sim1-81 Oct 02 '19 at 14:34
  • The response is json. But i dont understand how or why i get the xml error. – developer n Oct 02 '19 at 14:45
  • if you use $.ajax() instead of $.getJSON() do you get the same error? – Sim1-81 Oct 02 '19 at 14:48
  • I have not tried as i was checking to fix it by content-type. But i can just try . – developer n Oct 02 '19 at 14:50
  • you can change the content-type in your call, but also the server which supply the response have to got the same content-type for his response. which is the error you get in console? may the response JSON can be corrupted or not well formatted? – Sim1-81 Oct 02 '19 at 14:57
  • How can i change in my call using $getjson itself as iam not able to bring to working using $ajax. – developer n Oct 02 '19 at 15:03
  • here the official docs of $.ajax() https://api.jquery.com/Jquery.ajax/ an coded example is this `jQuery.ajax({ type: 'POST', url: url + node.id, data: your_call_papameters_object, dataType: 'json', success: function (data) { console.log(data); }, error: function (xhr, ajaxOptions, thrownError) { console.log('error: ' + xhr + ajaxOptions + thrownError); }` – Sim1-81 Oct 02 '19 at 15:11
  • This also doesnt work unfortunately :( – developer n Oct 08 '19 at 08:34

1 Answers1

1

I solved the issue by adding the foolowing inside beforeSend :

jqXHR.overrideMimeType("application/json;charset=UTF-8");