Minichua
Minichua

Reputation: 177

jQuery AJAX get/post working erratically on one of my page

I have several AJAX calls on my website that are working perfectly. However, I am trying to create a simple email subscription on the sidebar using AJAX to submit the email with this script (stored in an external file):

jQuery(document).on( 'click', '#news_button', function() {
    var news_email = jQuery("#news_email").val();
    var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
    if (pattern.test(news_email)) {
        jQuery.post( "subscribe.php", {email: news_email}, function( data ) {
            alert('sent');
        });
    }
});

The Html code is very simple with two inputs (one email, one button).

When I trace the execution, I can see that the jQuery script gets the email correctly and that the subscribe.php file is called. But, what goes wrong: - with POST method, the parameter email is never transmitted - with GET method, the request is repeated (and received) almost 20/30 times with a 302 status instead of one interaction with the server.

I really do not know where it could come from. Can you help?

Upvotes: 0

Views: 72

Answers (1)

Vipul Hadiya
Vipul Hadiya

Reputation: 882

Are you sure you are running your website on localhost or actual web server? Because if your HTML file is open with file:// protocol in browser than POST will not work. HttpRequestObject works only with client-server architecture.

Be sure about http:// protocol in your browser.

Upvotes: 1

Related Questions