简体   繁体   English

如何防止多个Ajax请求?

[英]How to prevent Multiple Ajax request?

I have spent 2 days to find a solution, but stackoverflow does not have correct answer for this. 我已经花了2天的时间找到解决方案,但是stackoverflow对此没有正确的答案。 I have 2 ajax function first loading values onload, 我有2个Ajax函数首先加载onload值,

$.ajax({
    url: 'http://localhost/movies/data/home_data.php',
    type: 'POST',
    dataType: 'json',
    cache: false,
    success: function(data) {

        var home_contents_data='';

        $.each(data, function(index, element) {

            home_contents_data += '<a href="single-movie.html" onclick="readSingleMovie2(\''+data[index].id+'\')">More Details</a>';

        });

    }
});

it is working and giving data perfectly. 它正在工作并完美地提供数据。 it have a onclick function call as "readSingleMovie2()" I want to send this value to another ajax function. 它有一个onclick函数调用,如“ readSingleMovie2()”,我想将此值发送给另一个ajax函数。 this is my second ajax function 这是我的第二个ajax函数

//second function
function readSingleMovie2(movie_id2)
{   

myApp.onPageInit('single-movie-2', function (page) {

var single_movie_details2 = '';

$.ajax({
    url: 'http://localhost/movies/data/single_movie-2.php?rand='+(Math.random()),
    type: 'POST',
    data: 'movie_id2='+movie_id2, 
    dataType: 'json',
    cache: false,
    success: function(data) {

        var single_movie_data='';
        $.each(data, function(index, element) {

            single_movie_data += '<div>'+data[index].film_name+'</div>';

        });

    }
});



})

}

That is also working perfectly and that function data comes inside to the function. 这也可以正常工作,并且功能数据进入功能内部。 but my problem is. 但是我的问题是 when I click second time some of value from 1st function. 当我第二次点击第一功能的一些价值。 second function's URL loading multiple times. 第二个函数的URL多​​次加载。 I have attached firebug screenshot image to get an idea. 我已附上萤火虫屏幕截图图像以了解一个想法。

在此处输入图片说明

I tried with unbind, preventDefault, preventStop and cache false... everything I know and get the internet. 我尝试解除绑定,preventDefault,preventStop并缓存false ...我所知道的一切并获得了互联网。 But I am still finding the better solution for this. 但是我仍然在寻找更好的解决方案。 Please help me to resolve this problem. 请帮助我解决此问题。

Really appreciate your valuable time and answers Thanks! 真的感谢您的宝贵时间并回答谢谢!

Instead of unbind, preventDefault and preventStop, try off() . 尝试取消off()而不是unbind,preventDefault和preventStop。

See this: http://api.jquery.com/off/ 看到这个: http : //api.jquery.com/off/

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

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