jQuery AJAX POST 转为 GET

6

我正在尝试使用jQuery将表单数据发送到当前页面以进行变量处理。当我发送POST请求时,服务器接收到GET请求并且我的URL更改以匹配该GET请求。我理解如果我没有显式使用type: "POST",jQuery会自动转换为GET请求,但我相信我已经这样做了。希望能得到任何帮助。

<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>

5
#domainform内没有表单。 - thebjorn
1个回答

8

你可能需要做的事情:

e.preventDefault()

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

加载页面时,为了避免提交表单覆盖ajax操作,可以使用preventDefault函数阻止默认行为;同时,也可以在form标签中添加form action属性:action="post"

1
首先,他需要将那个 onclick= HTML 属性转换为 .on('click',function(e) {...}) - Blazemonger
2
这是因为他没有直接将函数附加到点击事件中,而是使用了onclick="return addadomain();和从函数本身返回return false。发现问题加1。 - Rory McCrossan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接