接受Ext.Ajax.request中的application/pdf [英] Accept application/pdf in Ext.Ajax.request
问题描述
我正在使用Ext.Ajax.request()
进行产生MediaType.MULTIPART_FORM_DATA
的API调用.这是在php
层中处理的,该层返回
I am using Ext.Ajax.request()
to make an API call which produces MediaType.MULTIPART_FORM_DATA
. This is being handled in the php
layer which returns the object in
header("Content-Type: application/pdf");
echo $response;
header("Content-Type: application/pdf");
echo $response;
问题是,我无法处理Ext.Ajax.request()
中的接收对象,因为默认情况下它始终只处理Json对象.
The problem is, I am not able to handle the received object in the Ext.Ajax.request()
since it always handles only Json objects by default.
我尝试在请求中提供Headers
,AcceptType
,但是它总是转到failure
块.
I tried giving Headers
, AcceptType
in the request, but it always goes to the failure
block.
这是代码:
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);
}
});
推荐答案
但它总是会失败
but it always goes to the failure
为此,您需要在php
端签入,因为可能您错过了某些内容.我认为这可能会帮助您 readfile
For this you need to check in php
side because may be something you have missed. I think may be this will help you readfile
尝试使用此代码.希望这会帮助/指导您获得所需的结果.
Try with this code. Hope this will help/guide you to get required result.
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.
}
});
希望这也可以帮助您解决问题也是如此.
Hope this will also help you for this question as well.
这篇关于接受Ext.Ajax.request中的application/pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!