$ .ajax发布在Chrome中,但不在Firefox中

时间:2022-12-06 21:09:51

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?

好的,我会做空的。我有这个脚本,它将值放在数据库中。它在Chrome,Safari中运行良好,但无法在Firefox或IE中运行。似乎数据甚至没有发布到.php文件,而ajax根本没有启动。有人吗?

This is my jquery script:

这是我的jquery脚本:

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

This is the content in my php file:

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

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

3 个解决方案

#1


18  

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

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

event.preventDefault();

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).

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

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

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

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

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.

还有一个区别:如果您没有定义事件参数,则事件将引用Chrome中的本机事件对象,这与jQuery传递给处理程序的事件对象不同。

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

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

#2


7  

Async call might not work in FF if you it triggered on form submission. You can add async:false to your ajax call and it will work. It is either that or the fact that you have cross domain call that you will have to fix through CORS.

如果您在表单提交时触发,则异​​步调用可能无法在FF中运行。您可以为您的ajax调用添加async:false,它将起作用。无论是您是否具有跨域调用,您必须通过CORS进行修复。

#3


1  

The Firefox missing the $ajax async call has been fixed in the Firefox v49.0.2 and above.

在Firefox v49.0.2及更高版本中修复了Firefox缺少$ ajax异步调用的问题。

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

The above code will work when you upgrade to Firefox v49.0.2 or above.

升级到Firefox v49.0.2或更高版本时,上述代码将起作用。

#1


18  

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

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

event.preventDefault();

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).

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

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

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

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

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.

还有一个区别:如果您没有定义事件参数,则事件将引用Chrome中的本机事件对象,这与jQuery传递给处理程序的事件对象不同。

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

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

#2


7  

Async call might not work in FF if you it triggered on form submission. You can add async:false to your ajax call and it will work. It is either that or the fact that you have cross domain call that you will have to fix through CORS.

如果您在表单提交时触发,则异​​步调用可能无法在FF中运行。您可以为您的ajax调用添加async:false,它将起作用。无论是您是否具有跨域调用,您必须通过CORS进行修复。

#3


1  

The Firefox missing the $ajax async call has been fixed in the Firefox v49.0.2 and above.

在Firefox v49.0.2及更高版本中修复了Firefox缺少$ ajax异步调用的问题。

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

The above code will work when you upgrade to Firefox v49.0.2 or above.

升级到Firefox v49.0.2或更高版本时,上述代码将起作用。