简体   繁体   English

使用jquery Ajax处理动态HTTP Content-Type

[英]Handling dynamic HTTP Content-Type with jquery Ajax

I have a perl script URL which gives me a ZIP file, it processes the data and dynamically set the Content-Type to 'application/zip' in the http-header. 我有一个perl脚本URL,它给我一个ZIP文件,它处理数据并动态地将Content-Type设置为http-header中的'application/zip'

Perl code looks like this: Perl代码如下所示:

WPHTTPResponse::setHeader( 'Content-disposition', 'attachment;filename="test.zip"');
WPHTTPResponse::setHeader( 'Content-type', 'application/zip');  
print $result;

In frontend, I am using this script direct in HREF: 在前端,我在HREF中直接使用此脚本:

      <a href="/script">Download</a>

But I have a requirement of showing an alert message if $result is NULL in perl script. 但是如果perl脚本中$result为NULL,我需要显示警告消息。

What I am thinking is: I will send the file with Content-Type=>application/zip if the $result if not null, otherwise I will send the JSON response with error message that there is no file to download. 我的想法是:如果$ result不为null,我将发送带有Content-Type=>application/zip的文件,否则我将发送JSON响应,并显示没有要下载的文件的错误消息。

For this I would need to dynamically check the content type using JS: 为此,我需要使用JS动态检查内容类型:

$.ajax({
  url: '/script',
  data: {....},
  success: function(response) {
     if(response.contentType == 'application/zip'){
        //download using javascript
     }
     else{
        $.parseJSON(response);
        alert(response.msg);
     }
  }
});

I'd appreciate any help. 我很感激任何帮助。

Although your method should work, this is a case where using using HTTP error codes would probably be a good choice. 虽然您的方法应该可行,但这种情况下使用HTTP错误代码可能是一个不错的选择。

Depending on why $result is null one of these should be appropriate. 根据$ result为null的原因,其中一个应该是合适的。

400 Bad Request
406 Not Acceptable
410 Gone

This would make your code slightly more 'obvious' as you would be using the HTTP status for exactly what it was meant for, rather than re-implementing the exact same thing in a proprietary way in your code. 这将使您的代码稍微更“明显”,因为您将HTTP状态用于其目的,而不是在代码中以专有方式重新实现完全相同的东西。 IMHO it would also make your code a bit easier to maintain as it would separate the success from the error. 恕我直言,它也会使你的代码更容易维护,因为它会将成功与错误分开。

You can still include JSON as part of the error response, to be able display information about exactly why the request didn't result in any data being returned to the client. 您仍然可以将JSON作为错误响应的一部分包含在内,以便能够显示有关请求未确切导致任何数据返回给客户端的确切原因的信息。

btw I'd avoid using 404 as the error code, even though it is technically the most 'appropriate' code just because it would cause confusion if a real 404 error occurred. 顺便说一下,我会避免使用404作为错误代码,即使它在技术上是最“适当”的代码,只是因为如果发生真正的404错误会导致混淆。

Use HEAD -request for check content size before download. 在下载之前使用HEAD -request检查内容大小。

Client-side: 客户端:

  • Attach click -event handler to <a> -element. click -event处理程序附加到<a> -element。
  • On click -event send HEAD -request throw XHR . click -event发送HEAD -request抛出XHR
  • On XHR -response check content size. XHR -response上检查内容大小。
  • If size is zero, then show alert and prevent default event handler. 如果size为零,则显示alert并阻止默认事件处理程序。
  • If size is not zero, nothing to do. 如果大小不为零,则无事可做。

Server-side: 服务器端:

  • Compute content size on HEAD -request. HEAD -request上计算内容大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM