Принять заявку / pdf в Ext.Ajax.request - PullRequest
0 голосов
/ 11 мая 2018

Я использую Ext.Ajax.request() для вызова API, который выдает MediaType.MULTIPART_FORM_DATA.Это обрабатывается в слое php, который возвращает объект в

header("Content-Type: application/pdf"); echo $response;

Проблема в том, что я не могу обработать полученный объект в Ext.Ajax.request(), так как онпо умолчанию всегда обрабатывает только объекты Json.

Я пытался дать Headers, AcceptType в запросе, но он всегда идет в блок failure.

Вот код:

Ext.Ajax.useDefaultXhrHeader = false;
var responseText;
Ext.Ajax.request({
    url: '/index.php/abc/xyz?value=' + value,
    method: 'GET',
    waitMsg: 'Printing Label',
    contentType: 'application/octet-stream' //not sure,
    responseType: 'blob' //not sure,
    xhr2: false //not sure,
    success: function (response) {
        console.log("Success!!");
        return "";
    },
    failure: function (response) {
        //Always comes here. The API returns 200
        console.log("Hi here in the error");
        //Raw pdf gets printed
        console.log(response.responseText);
    }
});

1 Ответ

0 голосов
/ 11 мая 2018

, но это всегда приводит к сбою

Для этого вам нужно проверить сторону php, потому что это может быть то, что вы пропустили.Я думаю, может быть, это поможет вам readfile

Попробуйте с этим кодом.Надеюсь, что это поможет / поможет вам получить требуемый результат.

Ext.Ajax.request({
    url: '/index.php/abc/xyz?value=' + value,
    method: 'GET',
    waitMsg: 'Printing Label',
    cors: true,
    useDefaultXhrHeader: false,
    withCredentials: true,
    defaultHeaders: {
        'Content-Type': 'application/octet-stream;'
    },
    timeout:0,//If don't know how time will take server to get the file then you can put 0. if you need 
    success: function(response) {
        //In response you will directly get the octet-stream

        //For showing pdf in front end side using blob url
        var byteCharacters = atob(response.responseText),
            len = byteCharacters.length,
            byteNumbers = new Array(len),
            key = 0,
            byteArray,
            blob,
            contentType = 'application/pdf';

        //insert charcter code in {byteNumbers}
        for (; key < len; key++) {
            byteNumbers[key] = byteCharacters.charCodeAt(key);
        }
        //convert {byteNumbers} into Uint8Array
        byteArray = new Uint8Array(byteNumbers);
        //create blob using {byteArray}
        blob = new Blob([byteArray], {
            type: contentType
        });
        // set {src} to {iframe}
        window.open(window.URL.createObjectURL(blob), '_blank');
    },
    failure: function(response) {
        //if somthing wrong in serverside then it will come in failure.
    }
});

Надеюсь, что это также поможет вам и в этом вопросе .

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...