开发者

Ajax get returns empty data

The data variable returns nothing when it should return "OK" or "EXISTS".

I have a template with an overlay effect. The income.html template has a form and a button "Add a new category", when you click on it a new window is displayed(overlay effect) with a tiny form.

income.html:

(document).ready(function(){ 
$("#new_cat").live("click", ( function() {      
    var cat_name = $("#nc").val();
    if (cat_name) {
        $.get("/cashflow/new_cat/0/", { name: cat_name, account: {{ account }} },
          function(data){
            if (data == "OK") {
                $("#id_category").val(cat_name);
            }
            if (data == "EXISTS") {
                var error = "The category already exists";
                alert(error);
            }
          });
    }
    else {
         var error = "Please enter a name";
         alert(error);
    }
}))  
});
</script>
...

<form>{% csrf_token %}
       <label for="name">Name:</label><input type="text" id="nc" />
       <input type="submit" value="Submit" id="new_cat" />
</form>

views.py:

@login_required
def income(request, account_name): 
    account = account_name
    if request.method == 'POST':
        form = TransForm(user=request.user, dat开发者_如何学Goa=request.POST)
        if form.is_valid():
            income = form.save(commit=False)
            income.type = 0
            income.account = Account.objects.get(
                            name = account_name,
                            user = request.user)
            income.name = form.cleaned_data['name']
            income.category = form.cleaned_data['category']
            income.save()
            uri = ("/cashflow/account/%s") % str(account_name)
            return HttpResponseRedirect(uri)

    else:
        form = TransForm(user=request.user)

    context = {
          'TransForm': form,
          'type': '0',
          'account': account, 
    }
    return render_to_response(
        'cashflow/income.html',
        context,
        context_instance = RequestContext(request),
    )

def new_cat(request, type):
    if request.method == u'GET':
        GET = request.GET
        if GET.has_key(u'name'):
            name = request.GET[u'name']
            account = request.GET[u'account']
            c = Category.objects.filter(name=name, account=account)
            if c:
                s = "EXISTS"
            else:
                c = Category(name = name, user = request.user, 
                        type = type, account = account)
                c.save()
                s = "OK"

    return HttpResponse(s)


Are you crossing domains? You cannot do cross-domain ajax requests. That includes subdomains.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜