drupal 7 ajax,ajax_deliver

这段代码展示了在处理包含文件上传的AJAX请求时,如何定制JSON响应以适应浏览器的IFRAME处理,特别是针对IE浏览器的兼容性问题。通过设置'Content-Type'为'text/html;charset=utf-8'并在HTML中嵌入JSON数据来防止浏览器扩展破坏JSON响应的完整性。
摘要由CSDN通过智能技术生成

function $page_callback_result) {

// Browsers do not allow JavaScript to read the contents of a user's local

// files. To work around that, the jQuery Form plugin submits forms containing

// a file input element to an IFRAME, instead of using XHR. Browsers do not

// normally expect JSON strings as content within an IFRAME, so the response

// must be customized accordingly.

// @see http://malsup.com/jquery/form/#file-upload

// @see Drupal.ajax.prototype.beforeSend()

$iframe_upload = !empty($_POST['ajax_iframe_upload']);

// Emit a Content-Type HTTP header if none has been added by the page callback

// or by a wrapping delivery callback.

if ('Content-Type'))) {

if (!$iframe_upload) {

// Standard JSON can be returned to a browser's XHR object, and to

// non-browser user agents.

// @see http://www.ietf.org/rfc/rfc4627.txt?number=4627

'Content-Type', 'application/json; charset=utf-8');

}

else {

// Browser IFRAMEs expect HTML. With most other content types, Internet

// Explorer presents the user with a download prompt.

'Content-Type', 'text/html; charset=utf-8');

}

}

// Let ajax.js know that this response is safe to process.

// Print the response.

$commands = $page_callback_result);

$json = $commands);

if (!$iframe_upload) {

// Standard JSON can be returned to a browser's XHR object, and to

// non-browser user agents.

print $json;

}

else {

// Browser IFRAMEs expect HTML. Browser extensions, such as Linkification

// and Skype's Browser Highlighter, convert URLs, phone numbers, etc. into

// links. This corrupts the JSON response. Protect the integrity of the

// JSON data by making it the value of a textarea.

// @see http://malsup.com/jquery/form/#file-upload

// @see http://drupal.org/node/1009382

print '' . $json . '';

}

// Perform end-of-request tasks.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值