Django的说is_ajax是假的上一个jQuery AJAX请求 [英] Django says is_ajax is false on a JQuery AJAX request

查看:189
本文介绍了Django的说is_ajax是假的上一个jQuery AJAX请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景: Chrome浏览器扩展使用jQuery来请求远程Django应用程序的响应。 Django的认识到,请求通过AJAX提交并回应你好AJAX!。我立足我锻炼过这个很好的例子。因为这个请求正在从Chrome扩展,请求正在取得跨站点,所以我使用了 @CSRF_exempt 装饰我的Django的看法。

问题:我的Django的看法是不承认的请求,作为一个AJAX请求,而是响应您好AJAX 它响应<$ C的! $ C>你好不AJAX!。

我的Django的观点:
(网址 / xhr_test 使用以下视图)

  @csrf_exempt
高清check_login_extension(要求):
    如果request.is_ajax():
        消息=你好AJAX!
    其他:
        消息=你好不AJAX
    返回的Htt presponse(消息)
 

我的JQuery的要求:

 函数xhrconnect(){
    $获得(HTTP://本地主机:8000 / xhr_test,功能(数据){
      的document.getElementById('xhrmsg')的innerHTML =(数据)。
    });
}
 

解决方案

去通过jQuery的来源,它看起来像 $。阿贾克斯()(因此 $。获得() $。员额()等),将自动设置跨域选项,如果它看到你正在做一个跨域请求,这你(的相关code这里)。而在实际的AJAX请求,jQuery将不会设置 HTTP_X_REQUESTED_WITH 报头Django的需求 is_ajax(),如果跨域设置(相关$ C $Ç这里)。

我认为解决这个问题的最简单方法是明确设置跨域

 函数xhrconnect(){
    $阿贾克斯({
        网址:HTTP://本地主机:8000 / xhr_test
        成功:功能(数据){
            的document.getElementById('xhrmsg')的innerHTML =(数据)。
        },
        跨域:假的
    });
}
 

如果不工作,你可以尝试使用 AJAX prefilter功能手动设置 HTTP_X_REQUESTED_WITH 头的请求。

Context: a chrome browser extension uses JQuery to request a response from a remote django app. Django recognizes that the request is made via AJAX and responds with "Hello AJAX!". I'm basing my exercise off this great example. Because this request is being made from a chrome extension, the request is being made cross site, so I've used the @CSRF_exempt decorator on my Django view.

Problem: My Django view is not recognizing the request as an AJAX request, and instead of responding Hello AJAX! it responds Hello not AJAX!.

My Django view:
(The url /xhr_test uses the following view)

@csrf_exempt
def check_login_extension(request):
    if request.is_ajax():
        message = "Hello AJAX!"
    else:
        message = "Hello not AJAX"
    return HttpResponse(message)

My JQuery request:

function xhrconnect() {
    $.get("http://localhost:8000/xhr_test", function(data) {
      document.getElementById('xhrmsg').innerHTML = (data);
    });
}

解决方案

Going through the jQuery source, it looks like $.ajax() (and therefore $.get(), $.post(), etc) will automatically set the crossDomain option to true if it sees that you're making a cross-domain request, which you are (relevant code here). And in the actual AJAX request, jQuery won't set the HTTP_X_REQUESTED_WITH header that Django needs for is_ajax() if crossDomain is set (relevant code here).

I think the easiest way to fix this is to explicitly set crossDomain to false:

function xhrconnect() {
    $.ajax({
        url: "http://localhost:8000/xhr_test", 
        success: function(data) {
            document.getElementById('xhrmsg').innerHTML = (data);
        },
        crossDomain: false
    });
}

If that doesn't work, you could try using an AJAX prefilter function to manually set the HTTP_X_REQUESTED_WITH header on the request.

这篇关于Django的说is_ajax是假的上一个jQuery AJAX请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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