简体   繁体   中英

How to call a method from controller more once time? AJAX

Sorry if the question is really dumb, however, I couldn't find how to solve it.

I have a very simple methid in a controller:

public string ExactSeconds()
{
   string str = DateTime.Now.Second.ToString();
   return str;            
}

The view is simpler than the method:

<p id="rData"></p> 
<p id="qqqqq">click me!</p> 

JavaScriptCode:

<script type="text/javascript">
  $(document).ready(function () {
    $('#qqqqq').click(function () {
        alert('');
        var url = "/Home/ExactSeconds";
        $.get(url, null, function (data) {                
                $("#rData").html(data);
            });

        });

    });
 });

However, When I click at id="qqqqq" , then it just uploads data(seconds) just for the first time. Then if I click the second time and the next times, then alert('') works perfectly, however it is not called method ExactSeconds, that is I cannot see updated seconds at the view.

How to call method ExactSeconds() always when I click at #qqqqq?

<script type="text/javascript">
  $(document).ready(function () {
    $('#qqqqq').click(function () {
        alert('');
        var url = "/Home/ProverbPartialView";
        $.get(url, null, function (data) {                
            $.get(url, null, function (data) {
                $("#rData").empty();
                $("#rData").html(data);
            });

        });

    });
 });

Try this by adding $("#rData")..empty(); in the code

When I remove your AJAX calls, this works fine.

 $(document).ready(function () { $('#qqqqq').click(function () { alert(''); $("#rData").html(Date.now); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <p id="rData"></p> <p id="qqqqq">click me!</p> 

Secondly, why do you have nested one AJAX get call inside another. How about just one call?

$(document).ready(function () {
    $('#qqqqq').click(function () {
        alert('');
        var url = "/Home/ProverbPartialView";
        $.get(url, null, function (data) {
            $("#rData").html(data);
        });
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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