如何检查是否一个Ajax响应的jQuery有HTML内容是什么? [英] How to check if an AJAX response has HTML contents in jQuery?

查看:125
本文介绍了如何检查是否一个Ajax响应的jQuery有HTML内容是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种形式和在一个成功的AJAX调用的事件的两个可能的响应,其中一个仅返回一个状态code进行页

I have a page with one form and two possible responses in the event of a successful AJAX call, one of which only returns a status code.

我需要做的是检查响应对象我的成功回调的任何HTML内容,这样我可以在我的页面上显示出来。

What I need to do is check the response object in my success callback for any HTML contents so that I can display them on my page.

我已经知道我可以将其添加为一个参数,像这样访问响应在我的回调:

I already know that I can access response in my callback by adding it as a parameter, like so:

函数成功(响应){}

我想不通的唯一的事情就是如何检查该对象是否具有任何HTML内容。我怎样才能做到这一点?

The only thing I can't figure out is how to check if that object has any HTML contents. How can I do this?

推荐答案

您可能想看看响应头一个HTML MIME类型。 $。阿贾克斯 将通过一个< A HREF =htt​​p://api.jquery.com/jQuery.ajax/#jqXHR相对=nofollow> jqXHR 对象返回到您的成功回调,然后你就可以调用<一个href="https://developer.mozilla.org/en-US/docs/Web/API/XMLHtt$p$pquest?redirectlocale=en-US&redirectslug=DOM%2FXMLHtt$p$pquest#getResponseHeader%28%29"相对=nofollow> .getResponseHeader() 的:

You probably want to look at the response headers for an HTML MIME type. $.ajax will pass a jqXHR object back into your success callback, which you can then call .getResponseHeader() on:

function success( response, status, jqXHR ) {
    if( jqXHR.getResponseHeader('content-type').indexOf('text/html') >= 0 ) {
        ...
    }
}

这篇关于如何检查是否一个Ajax响应的jQuery有HTML内容是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆