0

我在登录时遇到以下问题。问题是该站点正在此脚本中使用 jQuery 和 ajax 处理表单:

function form_login()
{
    if($('#main form p.button.disabled').length) return false;

    $('#success').hide();
    $('#error').hide();

    $.post( AJAX_PATH + '/login/', $(form).serialize(), function(data) {


        if(data.result) {

            $('#success').html(data.response).show();
            setTimeout(function() { redirect(data.redirect); }, 3000);
        }
        else {

            $('#error').html(data.response).show();
            $('#login').val('');
            $('#password').val('');
            $('#main form p.button').addClass('disabled');
        }

    }, 'json');

    return false;
}

我尝试使用以下代码使用 Mechanize 手动发布:

h = Http()
data = dict(login=user, password=password)
resp, content = h.request(url, "POST", urlencode(data))
print "Post response: " , resp

响应如下:

{'status': '200', 'content-length': '3257', '-content-encoding': 'gzip', 'transfer-encoding': 'chunked', 'vary': 'Accept-Encoding', 'server': 'nginx', 'last-modified': 'Fri, 15 Mar 2013 14:40:44 GMT', 'connection': 'keep-alive', 'etag': '"11c2203d-cb9-4d7f79fc19300;4d7f7b508f640"', 'date': 'Sun, 28 Apr 2013 23:15:07 GMT', 'content-type': 'text/html'}

我现在该怎么办?

4

1 回答 1

0

Ok, I figured out, that I have already been logged in with my code. But when I go to another page I loose my login state. So I tried not completely make a manual POST, but use mechanize. Because then it handles cookies and everything by itself. Here is what I got:

LoginUrl = "http://mywebsite.com/ajax/login/"
LoginData = dict(login=user, password=password, remember=1)
LoginHeader = {"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:20.0) Gecko/20100101 Firefox/20.0"}
LoginRequest = urllib2.Request(LoginUrl, urlencode(LoginData))
LoginResponse = br.open(LoginRequest)

And it WORKS! :-D

于 2013-04-29T08:29:53.430 回答