简体   繁体   English

为什么这个Jquery Ajax无法在Opera和Safari中运行?

[英]Why isn't this Jquery Ajax working in Opera and Safari?

This code works in FF, Chrome, IE6/8 but not in Safari and Opera. 该代码在FF,Chrome,IE6 / 8中有效,但在Safari和Opera中无效。 Any ideas why? 有什么想法吗?

Here is the code: 这是代码:

var name = $('#esm').val();
        var email = $('#nam').val();
        var message = $('#med').val();
        var ad_id = $('#i_d').val();

    var data_string = 'esm='+ name + '&nam=' + email + '&med=' + message + '&i_d=' + ad_id;

            $.ajax({
                type:       "POST",
                url:        "/my_php_file.php",
                data:       data_string,
                success:    function(data) {
                    $('#tip_loader').hide();
               if(data==1){alert('success'); }
               else {alert('error'); }
                       }//end success function
        }) //end ajax call

I have located the error to exactly the "Ajax" call, because when I put an alertbox just before the $.ajax the alert shows up correctly. 我将错误定位在“ Ajax”调用上,因为在$.ajax之前放置警报框时,警报可以正确显示。 However, if I put the alertbox in the success function, nothing shows up, no alert. 但是,如果将警报框放在成功功能中,则什么也没有显示,也没有警报。

This only happens in Opera and Safari... 这仅发生在Opera和Safari中...

EDIT: 编辑:

FYI: I include this javascript file into a php file, and I also include the jquery.js file into the php file. 仅供参考:我将此JavaScript文件包含在php文件中,并且还将jquery.js文件包含在php文件中。 So this is all in an external file. 所以这一切都在一个外部文件中。

EDIT: 编辑:

/main.php /bin/jquery.js /bin/tip.js /bin/tip.php /main.php /bin/jquery.js /bin/tip.js /bin/tip.php

I include the above js files into main.php, and the form action in main.php is set to /bin/tip.php 我将上述js文件包含在main.php中,并且main.php中的form action设置为/bin/tip.php

And the path to the ajax url is /bin/tip.php instead of my_php_file.php 而且Ajax网址的路径是/bin/tip.php而不是my_php_file.php

In Opera, by default Allow File XMLHttpRequest is false. 在Opera中,默认情况下,“允许文件XMLHttpRequest”为false。 So you need to change the settings. 因此,您需要更改设置。 Open Opera browser, type about:config. 打开Opera浏览器,输入about:config。 It will take you to the Preference screen. 它将带您到“首选项”屏幕。 Go to User Prefs folder, you can see a settings Allow File XMLHttpRequest. 转到User Prefs文件夹,您可以看到一个设置Allow File XMLHttpRequest。 Check that and then Save. 检查并保存。 It should work. 它应该工作。

Opera has a debugging tool built in called Dragonfly. Opera具有内置的调试工具Dragonfly。 Go to the Tools menu -> Advanced ->Opera Dragonfly 转到“工具”菜单->“高级”->“ Opera Dragonfly”
If you don't have the File menu bar, click Menu -> Page -> Developer Tools -> Open Opera Dragonfly 如果没有文件菜单栏,请单击菜单->页面->开发人员工具->打开Opera Dragonfly

Once you have it open (open it on the page that you're working on), click the Scripts tab (it'll probably ask you to refresh the page, do that) and drop down to your external js file. 打开它后(在您正在处理的页面上打开它),单击Scripts选项卡(它可能会要求您刷新页面,执行此操作),然后下拉至您的外部js文件。 Once you've found your code, you can set a breakpoint on the $.ajax() line by clicking on the line number on the left side. 找到代码后,可以通过单击左侧的行号在$.ajax()行上设置断点。 Now, trigger your code and you'll see that it will break on that JavaScript line. 现在,触发您的代码,您将看到它会在该JavaScript行上中断。 You can then use the inspection tab (bottom, middle) to ensure that all of your variables are set correctly. 然后,您可以使用检查选项卡(底部,中间)来确保正确设置了所有变量。 You can also step through and debug the JavaScript. 您也可以单步调试JavaScript。

The other thing you'll want to do is add an error function like so: 您要做的另一件事是添加一个错误函数,如下所示:

$.ajax({
    type: "POST",
    url: "/my_php_file.php",
    data: data_string,
    success: function(data) {
        $('#tip_loader').hide();
        if (data == 1) { alert('success'); }
        else { alert('error'); }
    }, //end success function
    error: function(xhr, textStatus, errorThrown) {
        alert(errorThrown);
    }
});  //end ajax call

See if that gives you any more information. 看看是否能为您提供更多信息。

Also, check the error console as @Mufasa suggested. 另外,按照@Mufasa的建议检查错误控制台。 It can be found under the Error Console tab in Dragonfly. 可以在Dragonfly中的“错误控制台”选项卡下找到它。

There isn't really a way to tell. 真的没有办法说出来。 You didn't post the php file. 您没有发布php文件。 Without knowing the output, we can't determine how the browser will respond. 在不知道输出的情况下,我们无法确定浏览器将如何响应。

Other tips though, #1 you are not using encodeURIComponent for any of the values being passed it. 但是,还有其他一些技巧,#1您没有为任何传递它的值使用encodeURIComponent。 Its much more simple to get jQuery to do it for you, 让jQuery为您做这件事要简单得多,

instead of data: data_string, you should have 而不是数据:data_string,您应该有

data: { esm: name, nam: email, med: message, "i_d": ad_id } 数据:{esm:名称,nam:电子邮件,med:消息,“ i_d”:ad_id}

jQuery will create the query string for you and correctly. jQuery将为您正确创建查询字符串。

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

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