简体   繁体   English

在AJAX请求后,URL会自动附加$ _GET参数

[英]URL gets appended with $_GET-parameters automatically after AJAX request

I have the following source: 我有以下来源:

$('#loginnow').click(function() {       
    var login_useroremail       = $('input[name="login_useroremail"]').val();
    var login_password          = $('input[name="login_password"]').val();

    $.ajax({
            type: "POST",
            url: "http://www.example.com/login.php",   
            data: {
                login_useroremail: login_useroremail,
                login_password: login_password              
            },
            dataType: 'text',
            success: function(data) {       
                if(data == 'ok')
                {
                    alert('Registration successful!');  
                }
                else
                {
                    alert('Registration failed. Try again later.'); 
                }

            },
            error: function(data) {
                alert('Registration failed. Try again later.');
            }
    });     
});

I know those alerts and conditions are nasty, but as I'm in an early project state, I will refine that later. 我知道这些警报和条件是令人讨厌的,但由于我处于早期项目状态,我将在稍后进行改进。 ;) I'm working with jQuery, jQuery Mobile and PHPTAL. ;)我正在使用jQuery,jQuery Mobile和PHPTAL。

When I call the example and login was indeed successful (=server returns "ok"), everything is finde. 当我调用示例并且登录确实成功(=服务器返回“ok”)时,一切都是finde。

My question: Why is it that when I call the example and login is not successful, in my URL bar the parameters are automatically appended? 我的问题:为什么当我调用示例并且登录不成功时,在我的URL栏中会自动附加参数? After the call it looks like: 通话后看起来像:

www.example.com/index.php?user=blah&pw=yadda&...

Sometimes it also has an anchor like 有时它也有像锚一样

www.example.com/index.php#ui-state=dialog

At least I can imagine what that is for, I think it helps remembering what you are currently doing in the mobile app. 至少我可以想象这是为了什么,我认为它有助于记住你目前在移动应用程序中做的事情。

OK, now here we go. 好的,现在我们走了。

I found out what was happening and it's a bit tricky. 我发现了发生了什么,这有点棘手。

When using the AJAX request above, I used data from a normal HTML-Form. 当使用上面的AJAX请求时,我使用了普通HTML表单中的数据。 In the form tag opening the HTML-form I hadn't provided an action- or method-attribute. 在打开HTML表单的表单标记中,我没有提供action-或method-attribute属性。 It was just like 就像

<form name="bla">
...
</form>

Now what happened? 现在发生了什么? I pressed the submit button of the form. 我按下了表单的提交按钮。 The jQuery-event got triggered. jQuery事件被触发了。 But at the same time, the normal form event also got triggered. 但与此同时,正常的形式事件也被触发了。 As I had set no action or method, it automatically assumed 因为我没有设置任何动作或方法,所以它会自动假设

<form name="bla" method="get">
...
</form>

That's what finished me off because the GET-paramers of course appeared in the URL afterwards. 这就是让我失望的原因,因为当然GET参数出现在URL中。 :) :)

I now use .preventDefault() in the jquery script to avoid the normal form getting submitted any other way than via AJAX. 我现在在jquery脚本中使用.preventDefault()来避免以通过AJAX之外的任何其他方式提交正常表单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM