开发者

Google Chrome: jquery.ajax fails within onclick-event ("failed to load resource")

I use Google Chrome 7.0.x and I have the following javascript-function that gets called when a user clicks a button:

function myFunction(myId) {
 $("#" + 开发者_JAVA百科myId).click(function() {
  $.ajax({
  type: "POST",
  url: "myURL",
  success: function(msg){
   alert (msg);
  }
 });
});

alert() gets never called, instead I get "failed to load resource" when I lock at the console. What am I doing wrong?


Instead of return false; you should do function (e) { and then immediately on the next line e.preventDefault(); to stop the default action of the click event and do whatever you tell it to do. This is much better practice because you will not always want to return false as a value.

function myFunction(myId) {
     $("#" + myId).click(function(e) {
         e.preventDefault();
         $.ajax({
            type: "POST",
            url: "myURL",
            success: function(msg){
                 alert (msg);
            }
     });
}    );


You forgot return false in the end of the click handler, didn't you?


Try this to see what the error is:

function myFunction(myId) {
 $("#" + myId).click(function() {
  $.ajax({
  type: "POST",
  url: "myURL",
  success: function(msg){
   alert (msg);
  },
   error: function (error, t, msg) {
    console.log(error);
        console.log(t);
        console.log(msg);
   }
 });
});

Maybe your URL is wrong


I bet your URL is wrong , please check url through Firebug net panel , it should fix the problem.

THere won't be any problem with chrome for the above code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜