简体   繁体   中英

window.location is not woking

I'm using jQuery with Django in server-side. What I'm trying to do is to get some text from the user through the form and simultaneously displaying the text in the canvas area like about.me and flavors.me does. Then the user drag the text in the canvas area to the desired position and when they click the next button,the data must be stored in the database and redirect to the homepage. Everything is working perfect(the datas are stored in the database) except when I click the button which I set window.location to "http://127.0.0.1:8000". But I'm not getting to that page when I click the button.

I'm getting some errors in Django server:

error: [Errno 32] Broken pipe
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 51161)
Traceback (most recent call last):
File "/usr/lib/python2.7/SocketServer.py", line 284, in _handle_request_noblock

Here is my html: https://gist.github.com/2359541

Django views.py:

from cover.models import CoverModel from django.http import HttpResponseRedirect

def coverview(request):
    if request.is_ajax():
            t = request.POST.get('top')
            l = request.POST.get('left')
            n = request.POST.get('name')
            h = request.POST.get('headline')
            try:
                    g = CoverModel.objects.get(user=request.user)
            except CoverModel.DoesNotExist:
                    co = CoverModel(top=t, left=l, name=n, headline=h)
                    co.user = request.user
                    co.save()
            else:
                    g.top = t
                    g.left = l
                    g.name = n
                    g.headline = h
                    g.save()
            return HttpResponseRedirect("/")

urls.py:

url(r'^cover/check/$', 'cover.views.coverview'),
url(r'^cover/$', login_required(direct_to_template), {'template': 'cover.html'}),

Could anyone help me?

Thanks!

There's really not enough information in your question to properly diagnose this, but you can try this:

It's always a bad idea to hard-code a domain name in your JS. What happens when you take this to production, for example? If you want to send the user to the homepage (presumed from the location being set to http://127.0.0.1:8000/ ), then set the location simply to / . That will ensure that it will always go to the site root regardless of the IP address, domain name or port.

Part of the problem is that you're trying to post data, and then immediately leaving the page by using window.location. You should only change the window.location whenever you get the response back from the $.post().

$.post("check/", { top: t, left: l, name: n, headline: h}, function(data) {
    window.location.href = "/";
});

Notice also that I removed the hardcoded URL. Use a relative one here, like Chris said.

If it still isn't working, you need to check for Javascript errors in the lines above. Use Firebug, Chrome Dev Tools, Opera Dragonfly, something. Check to make sure your POST is actually going through, and post more data about that back here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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