$.ajax 帖子在 Chrome 中有效,但在 Firefox 中无效 [英] $.ajax post working in Chrome, but not in Firefox

查看:30
本文介绍了$.ajax 帖子在 Chrome 中有效,但在 Firefox 中无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我会很矮.我有一个将值放入数据库的脚本.它在 Chrome、Safari 中运行良好,但无法在 Firefox 或 IE 中运行.似乎数据甚至没有发布到 .php 文件中,ajax 也根本没有启动.请问有人吗?

Okay, I'll be short. I have this script which is putting values in database. It's working perfect in Chrome, Safari, but can't make it work in Firefox or IE. It seems that data isn't even being posted to .php file and ajax is not starting at all. Anyone, please?

这是我的 jquery 脚本:

This is my jquery script:

$(document).ready(function(){
$("#dodaj").click(function(){
  event.preventDefault();
  var kategorija = $("#kategorija option:selected").val();
  var si = $("#si").val();
  var hu = $("#hu").val();
  var de = $("#de").val();
  var an = $("#an").val();
  var hr = $("#hr").val();

$.ajax({
    type: "POST",
    url: "dodaj_v_bazo.php",
    data: {"kategorija": kategorija, "si": si, "hu": hu, "de": de, "an": an, "hr": hr},
    success: function(data){
        alert( "Jed uspešno dodana."+data);
    }, 
});
return false;
});
});

这是我的php文件中的内容:

This is the content in my php file:

$kategorija = $_POST['kategorija'];
$si = $_POST['si'];
$hu = $_POST['hu'];
$de = $_POST['de'];
$an = $_POST['an'];
$hr = $_POST['hr'];

$dodaj_v_bazo = "INSERT INTO jedi (kategorija, si, hu, de, an ,hr) VALUES ('$kategorija', '$si', '$hu', '$de', '$an', '$hr')";
mysql_query($dodaj_v_bazo) or die(mysql_error());

推荐答案

您没有将 event 定义为事件处理程序的参数,因此在

You didn't define event as parameter of the event handler, hence in

event.preventDefault();

浏览器尝试在全局范围内查找 event.Chrome 恰好在全局范围内提供事件对象(因此没有错误),但 Firefox 没有(因此出现错误).

the browser tries to look up event in the global scope. Chrome happens to provide the event object in global scope (hence no error) but Firefox doesn't (hence an error).

我建议将 event 参数添加到事件处理程序中:

I'd suggest to add the event parameter to the event handler:

$("#dodaj").click(function(event){
    event.preventDefault();
    // ...
});

还有一个区别:如果没有定义event参数,event会引用Chrome中的native事件对象,这与 jQuery 传递给处理程序的事件对象不同.

There is an additional difference: If you don't define the event parameter, event will refer to the native event object in Chrome, which is different than the event object which jQuery passes to the handler.

要了解有关使用 jQuery 处理事件的更多信息,我建议阅读这些文章.

To learn more about event handling with jQuery, I recommend to go through these articles.

这篇关于$.ajax 帖子在 Chrome 中有效,但在 Firefox 中无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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