Zabs
Zabs

Reputation: 14142

CasperJS AJAX form via POST not working

I am trying to get CasperJS to post a form using AJAX - it doesn't seem to work for me, the code is below:

this.then(function() {
    response = this.evaluate(function() {
        params = $("#offer").serialize();

        //require('utils').dump(params);
        $.ajax({
            type: "POST",
            url: 'http://www.example.com/getoffer.php',
            data: params,
            success: function (data) {
                //return data.responseText;
                return __utils__.sendAJAX(url, 'POST', params);
            },
            error: function (xhr,status,error){
                return error;
            }

        });
    });

    this.echo(response);
});

Upvotes: 1

Views: 2341

Answers (1)

Darren Cook
Darren Cook

Reputation: 28968

CORS?

(I'm so tempted to just leave that as my shortest ever StackOverflow answer :-)

Your JavaScript is being executed from inside the browser, and the security model will apply. Your "origin" will be the page that CasperJS is requesting; if that is not "www.mysite.com" (or if it is but is HTTPS), then the browser will refuse to send it.

This answer https://stackoverflow.com/a/16221536/841830 says --web-security=false (give that as a casperjs commandline option) will get around CORS restrictions.

This issue seems to be doing the same as you, so if it is not a CORS problem, it might give you some other ideas: http://code.google.com/p/phantomjs/issues/detail?id=28

Upvotes: 2

Related Questions