Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery AJAX POST turns into GET

I am trying to send form data to the current page for variable handling using jQuery. When I send the POST request, the server receives a GET request and my URL changes to match that GET. I understand that jQuery automatically turns to a GET request if I don't implicitly use type: "POST", but that is what I believe am doing. Any help would be appreciated.

<script>
    function addadomain(){
        $.ajax({
            type: "POST",
            url: "domains.php",
            data: $("#domainform form").serializeArray(),
            success: function() {
                $("#domainlist").load("domains.php #domainlist");
                $.ajaxSetup({ cache: false });
            }
        });
        return false;
    };
</script>

<a id="displayText" href="#" onclick="toggle();" >Add Domains</a> 

<div id="toggleText" style="display: none">
    <form name="adddomain" id="domainform" autocomplete="on">
        <input type="hidden" name="userid" id="userid" value="<?PHP echo $loggedInUser->user_id; ?>" />

        Domain: <input type="text" name="domain" id="domain" required="required" /><br />
        Keyword: <input type="text" name="keyword" id="keyword" required="required" /><br />
        Facebook Url: <input type="text" name="facebook" id="facebook" /><br />
        Twitter Url: <input type="text" name="twitter" id="twitter" /><br />
        Analytics ID#: <input type="text" name="analytics" id="analytics" /><br />
        Blogspot Url: <input type="text" name="blogspot" id="blogspot" /><br />

        <input type="submit" onclick="addadomain()" id="submit" value="submit" />
    </form> 
</div>
like image 726
Danejir Avatar asked May 22 '12 20:05

Danejir


1 Answers

you might need to do:

e.preventDefault()

http://api.jquery.com/event.preventDefault/

when loading the page so that your submit doesnt to its default action on top of your ajax call...also, you could add a form action in your form tag action="post"

like image 67
raddrick Avatar answered Oct 13 '22 02:10

raddrick