开发者

Handling captcha challenge inside a jQuery.ajax script

This jQuery calls a script that scrapes a page and returns a json array back to the jQuery. However, in certain cases, the cURL script results in a redirect to a captcha challenge page (see the cURL in the class script).

Once I've determined that the cURL page is a captcha (rather than the intended page), how can I redirect the captcha to the user?

Should I just extract the captcha image and the form's action parameters and hidden fields and recreate the form to allow the user to submit it?

jQuery('#myDiv a').click
    (
        function()
        {       
            jQuery('#loader').show();

            var result='';

            jQuery.ajax
            ({
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                url: getPage.php,
                success: function(data)
                {       
                    if(data['captcha'])
                    {
                        //need to load the captcha page for the user to complete
                        return;
                    }
                }
            });
        });


//Contents of getPage.php

    class loadPageCurl {

        function loadPage($url, $headonly = TRUE ){

        $agents = 'Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.204 Safari/534.16';

        $ch = curl_init();
        curl开发者_C百科_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_VERBOSE, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
        curl_setopt($ch, CURLOPT_USERAGENT, $agents);
        curl_setopt($ch, CURLOPT_URL, $url);

        $curlResp = curl_exec($ch);
        curl_close($ch);

        //check to see if captcha or regular page and process accordingly
        if(//page is captcha)
        {
            //Redirect the html stream to the user for captcha completion
        }
        else
        {
        return $curlResp;
        }
    }


http://api.jquery.com/jQuery.get/ must help.

$.get('ajax/test.html', function(data) {
    $('.result').html(data);
    alert('Load was performed.');
});

Can load contents from the file and put it into a div or something like that.

    if(//page is captcha)
    {
            $.get('captcha.html', function(data) {
                $('.captchaDiv').html(data);
            });
    }

This may solve your problem.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜