Django的AJAX JSON响应出现在浏览器的原始文本 [英] Django AJAX JSON response appears as raw text in browser

查看:246
本文介绍了Django的AJAX JSON响应出现在浏览器的原始文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发使用Django(1.4.2)的条带Web应用程序内的Pinax框架和:

I'm developing a Stripe webapp using Django (1.4.2) inside a Pinax framework and with:

  1. Django的条纹支付
  2. eldarion阿贾克斯

我已经得到了一切,除了Ajax响应工作(JSON格式),似乎没有任何AJAX回调进行处理,因此出现在浏览器中的原始JSON数据:

I've got everything working except the ajax response (in JSON format) doesn't appear to be processed by any ajax callbacks, and hence appears as raw JSON data in the browser:

{"html": "\n\n<div class=\"change-card\">\n    <h2>Current Card</h2>\n    <p class=\"lead\">\n        \n            Current card on file is a <strong>Visa</strong>\n            ending in the digits <strong>4242</strong>.\n        \n    </p>\n    \n    \n    \n    <form action=\"/payments/a/change/card/\" data-stripe-key=\"\" class=\"form ajax\" data-replace-closest=\".change-card\" method=\"POST\">\n        <div style='display:none'><input type='hidden' name='csrfmiddlewaretoken' value='<>' /></div>\n        <input name=\"stripe_token\" type=\"hidden\" />\n        <a href=\"\" class=\"btn btn-primary change-card\">Change Card</a>\n    </form>\n</div>\n"}

<一个href="http://stackoverflow.com/questions/21774731/django-stripe-paymants-and-eldarion-ajax-subscribe-json-response">This SO质疑似乎是相似的,但目前还没有正式答复,并提交似乎已经理解了它(的意见去),但建议不要为我工作。

This SO question seems to be similar, but there is no official answer, and the submitter seems to have figured it out (going by the comments), but the suggestions don't work for me.

详细信息:

我用蟒蛇manage.py runserver命令无论Chrome和Firefox中进行测试。

I'm using python manage.py runserver within both Chrome and Firefox to test.

我刚开始使用的条纹的应用程序,并只用从Django的条纹支付的例子让去之前,我量力而为我的特殊需求。

I'm just getting started with the stripe app, and have just used the examples from django-stripe-payments to get going before I tailor for my specific needs.

这是我正在我的测试案例包括使用从Django的条纹支付标准的AJAX形式更改存储的信用卡:

My test case that I'm running involves using the standard ajax form from django-stripe-payments to change the stored credit card:

<form action="{% url 'payments_ajax_change_card' %}" data-stripe-key="{{ STRIPE_PUBLIC_KEY }}" class="form ajax" data-replace-closest=".change-card" method="POST">
    {% csrf_token %}
    <input name="stripe_token" type="hidden" />
    <a href="" class="btn btn-primary change-card">{% if request.user.customer.card_kind %}Change{% else %}Add{% endif %} Card</a>
</form>

有一个在我的基础模板一些JavaScript被调用,并从条纹显示的形式输入的信用卡资料 - 当选择了变牌按钮。我不认为这是错 - 我把它直接从的这里

我有一个以上功能如下:

I have the following above that function:

<script src="{% static "js/jquery-1.9.1.min.js" %}"></script>
<script src="//checkout.stripe.com/v2/checkout.js"></script>

这下面

<script src="{% static "js/eldarion-ajax.min.js" %}"></script>

继令牌的以下观点code执行返回(以及随后的表单提交事件):

Following the return of the token (and the subsequent form submit event) the following view code is executed:

@require_POST
@login_required
def change_card(request):
    try:
        customer = request.user.customer
        send_invoice = customer.card_fingerprint == ""
        customer.update_card(
            request.POST.get("stripe_token")
        )
        if send_invoice:
            customer.send_invoice()
        customer.retry_unpaid_invoices()
        data = {}
    except stripe.CardError, e:
        data = {"error": e.message}
    return _ajax_response(request, "payments/_change_card_form.html", **data)

再次...这是即开即装即用的Django条纹支付code。接下来发生的事是上述原料JSON。

Again... this is out-of-the-box django-stripe-payment code. The next thing that happens is the raw JSON as mentioned above.

推荐答案

好吧,我解决了这个问题。原来,它的确是一个jQuery的问题。我引用我适用的两个jQuery的文件。当我删除了第二个,坚持了在这个问题上面提到的一个,它的工作!我的信用卡信息更新预期在页面上(尽管速度缓慢)。

Ok, I solved it. It turned out it was indeed a jQuery problem. I was referencing two jQuery files in my application. When I removed the second one, and stuck with the one mentioned in the question above, it worked! My credit card details updated as expected on the page (albeit slowly).

由于我使用的是pinax框架,它会自动带有一个jQuery参考。它要求在least.This Django的调试工具栏包含在base.html文件中:

Because I am using the pinax framework, it comes with a jquery reference automatically. It's required for the Django debug toolbar at least.This is included in the "base.html" file inside:

{% block script_base %}{% endblock %}

(base.html就设在这里:pinax_theme_bootstrap /模板/ theme_bootstrap)

("base.html" is located here: pinax_theme_bootstrap/templates/theme_bootstrap)

幸运的是base.html提供过ridable称为jquery_src块,所以在我的应用我的site_base.html的文件,我刚刚进入向底部以下内容:

Fortunately base.html provides an over-ridable block called "jquery_src", so in my "site_base.html" file in my app, I just entered the following towards the bottom:

{% block jquery_src %}{% endblock %}

这去除第二jQuery库。

This removed the second jQuery library.

我也尝试过使用jQuery的v1.11.0这被认为是有过错的<一个href="http://stackoverflow.com/questions/21774731/django-stripe-paymants-and-eldarion-ajax-subscribe-json-response">this SO问题,我刚才提到,它也工作了。

I also tried using jQuery v1.11.0 which was thought to be at fault in this SO question I mentioned earlier and it also worked.

这篇关于Django的AJAX JSON响应出现在浏览器的原始文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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