jQuery ajax调用在Mac Safari和Chrome浏览器上返回空错误

时间:2022-10-08 08:38:33

I have been searching for solutions and trying fixes for days now but with no change.

我一直在寻找解决方案并尝试修复几天,但没有任何变化。

The boss man is on a Mac and I do not have one so I am having him try repeated fixes and relaying the output to me. So far no luck.

老板的人在Mac上我没有,所以我让他尝试重复修复并将输出转发给我。到目前为止没有运气。

The current setup is thus, I have a form with username and password inputs which is submitted after it goes through validation. The validation is an ajax call which validates the username and password then submits the form.

因此,当前的设置是,我有一个带有用户名和密码输入的表单,在经过验证后提交。验证是一个ajax调用,它验证用户名和密码然后提交表单。

Only on Mac Chrome and Safari (webkit) I get an error response from the ajax call which is blank.

仅在Mac Chrome和Safari(webkit)上,我收到来自ajax调用的错误响应,该响应为空。

I have stripped the php function that does the validation to simply echo "hello" so that I know it is not the php that is causing the problem.

我已经剥离了执行验证的php函数,只需回显“hello”,这样我就知道它不是导致问题的php。

So once the target element is clicked I grab the username and password values and send the ajax request. (which should hit my php file and just echo hello). However instead it returns and error with no message. What I mean by that is the xhr responseText and thrownError are all just blank/null.

因此,一旦点击了目标元素,我就会获取用户名和密码值并发送ajax请求。 (这应该打我的PHP文件,只是回声你好)。但是它返回并且没有消息而出错。我的意思是xhr responseText和thrownError都只是空白/ null。

so here is the code: (HTML)

所以这里是代码:(HTML)

<form id="signin_form" method="POST" action="index.php?cmd=Signin">
        <table id="welcomeSignin">
            <tr>
            <td>
                    <div class="welcome"><span class="signin-title-text f-xxlarge-or-link-goth">Welcome Back!</span></div>
                    <div id="error_msg"></div>
                    <table id="input_field_table">
                        <tr>
                                <td class="input-field-cell">
                                    <div class="singin-titles f-medium-gr-bold">Username</div>
                                </td>
                                <td class="input-field-cell">
                                    <div class="signin-inputs"><input class="signin styled" type="text" name="user_name"></div>
                                </td>
                        </tr>
                        <tr>
                            <td class="input-field-cell">
                                <div class="singin-titles f-medium-gr-bold">Password</div>
                            </td>
                            <td class="input-field-cell">
                                <div class="signin-inputs"><input class="signin styled" type="password" name="user_pass"></div>
                                <div class="forgot"><a href="" class="forgot-password f-small-or-link">Forgot your password?</a></div>
                            </td>
                        </tr>
                        <tr>
                            <td colspan="2">
                                <input type="hidden" name="post_type" value="signin">
                                <span id="create"><span class="f-medium">Don't have an account? <a href="" class="create"><span class="f-medium-or-link">click here</span></a></span></span> &nbsp;
                                <span class="active-login"><input type="checkbox" class="signin-radio" name="active_login"> <span class="f-medium"> Keep me signed in</span></span>
                                <div id="sign_in"><span class="signin-submit-button"><img src="../images/ready-create-icon.png"></span> <span class="signin-submit-text f-xxxlarge-gr-title-link">Sign In!</span></div>
                            </td>
                       </tr>
                    </table>
                </td>
            </tr>
    </table>

Nothing special there. Here is the jquery piece:

没什么特别的。这是jquery篇:

$('#sign_in').click(function(){
    var url = window.location.href;
    $('#error_msg').html('');
    var username = $('input[name="user_name"]').val();
    var password = $('input[name="user_pass"]').val();
    var err = '';
    $.ajax({
        url: ajaxUrl+'validate_signin',
        type: 'GET',
        cache: false,
        data: { username: encodeURIComponent(username), password: encodeURIComponent(password)},
        success: function(data, status, xhr){
            if(data !== 'OK'){
                err = data;
                $('#error_msg').html(err);
                return false;
            }else{
                $('#signin_form').submit();
                return false;
            }
        },
        error: function(xhr, ajaxOptions, thrownError){
            var tmp_err = "";
            tmp_err = ":: "+xhr.responseText+" :: "+xhr.responseXML+" :: ";
            alert(tmp_err);
            alert(xhr.responseText);
            alert(thrownError);
            window.location.href = url;
            }
        });
        return false;
});

the php file is just: echo "hello"; exit; just to avoid any errors for testing.

php文件只是:echo“hello”;出口;只是为了避免任何测试错误。

A few things here, I added the encodeURIComponent() because I did see a post describing a problem with special characters and white space.(didn't work) on success: I included the status and xhr vars just for debugging normally I just have data.

这里有一些东西,我添加了encodeURIComponent(),因为我确实看到一篇文章描述了特殊字符和空格的问题。(没有成功)成功:我包含了状态和xhr vars只是为了正常调试我只是有数据。

Now in error: this is what is coming up each time however all responses are blank, or undefined. I was hoping to get some kind of decent error but I get nothing. Now this is only on a Mac recently upgraded to Mountian Lion and only on Webkit (Chrome/Safari) browsers.

现在是错误的:这是每次都会出现的情况,但所有响应都是空白或未定义的。我希望得到一些体面的错误,但我一无所获。现在,这只是在最近升级到Mountian Lion的Mac上,而且仅在Webkit(Chrome / Safari)浏览器上。

I tried doing just XMLHttpRequest without using jQuery and I get the same thing.

我尝试在不使用jQuery的情况下只使用XMLHttpRequest,我得到同样的东西。

This works in IE, Firefox, Chrome and Safari on a PC, it also works in FireFox on his Mac.

这适用于PC上的IE,Firefox,Chrome和Safari,它也适用于Mac上的FireFox。

Finally this was working for him previously, I believe before his upgrade.

最后这对他来说很有用,我相信在他升级之前。

Any help would be great. I am pulling my hair out, I am greener on jQuery than php so hopefully one of you can help me out.

任何帮助都会很棒。我把我的头发拉出来,我在jQuery上比php更环保,所以希望你们中的一个可以帮助我。

1 个解决方案

#1


3  

This was a configuration issue. The problem occurred if the domain was entered without the leading www. on the domain name.

这是一个配置问题。如果在没有前导www的情况下输入域,则会出现问题。在域名上。

Adding header('Access-Control-Allow-Origin: *'); to the php file solves this. You can specify specific domains to allow or use *.

添加标题('Access-Control-Allow-Origin:*');到php文件解决了这个问题。您可以指定允许或使用*的特定域。

#1


3  

This was a configuration issue. The problem occurred if the domain was entered without the leading www. on the domain name.

这是一个配置问题。如果在没有前导www的情况下输入域,则会出现问题。在域名上。

Adding header('Access-Control-Allow-Origin: *'); to the php file solves this. You can specify specific domains to allow or use *.

添加标题('Access-Control-Allow-Origin:*');到php文件解决了这个问题。您可以指定允许或使用*的特定域。