jQuery ajax调用在Mac Safari和Chrome浏览器上返回空错误 [英] jQuery ajax call returns empty error on Mac Safari and Chrome browsers

查看:111
本文介绍了jQuery ajax调用在Mac Safari和Chrome浏览器上返回空错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

boss man在Mac上,我没有,所以我让他尝试反复修复并将输出中继给我.到目前为止没有运气.

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.

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

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.

仅在Mac Chrome和Safari(webkit)上,我从ajax调用中得到了错误响应,该响应为空.

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

我已经剥离了执行验证的php函数,以简单地回显"hello",以便我知道不是导致问题的php.

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.

因此,一旦单击目标元素,我将获取用户名和密码值并发送ajax请求. (这应该打我的php文件,只是回声你好).但是,它返回并显示错误,没有消息.我的意思是xhr responseText和thrownError都为空白/空.

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.

这是代码:(HTML)

so here is the code: (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>

那里没什么特别的.这是jquery片段:

Nothing special there. Here is the jquery piece:

$('#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;
});

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

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

这里有几件事,我添加了encodeURIComponent(),因为我确实看到了一篇描述特殊字符和空格的问题的文章.(成功的话):我包括了status和xhr vars,仅用于正常调试我只有数据.

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.

现在出错:这是每次都会出现的错误,但是所有响应均为空白或未定义.我希望得到某种体面的错误,但一无所获.现在,仅在最近升级到Mountian Lion的Mac上,以及仅在Webkit(Chrome/Safari)浏览器上.

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.

我尝试仅使用XMLHttpRequest而不使用jQuery,但我得到了同样的东西.

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

这可以在PC上的IE,Firefox,Chrome和Safari中使用,也可以在Mac上的FireFox中使用.

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

最后,这以前对他有用,我相信在他升级之前.

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

任何帮助都会很棒.我正在拔头发,在jQuery上比在PHP上更绿色,因此希望你们中的一个可以帮助我.

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.

推荐答案

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

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

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

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

这篇关于jQuery ajax调用在Mac Safari和Chrome浏览器上返回空错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆