在href中调用Ajax请求函数。

时间:2022-09-25 23:59:34

I have an href in an html page and i have an AJAX request in a method in a javascript file.

在html页面中有一个href,在javascript文件中的方法中有一个AJAX请求。

When clicking on href i want to call the JS function and I am treating the response to add it to the second html page which will appear

当单击href时,我想调用JS函数,并将响应添加到将要出现的第二个html页面。

function miniReport(){

    alert('TEST');

   var client_account_number = localStorage.getItem("numb");

   var request = $.ajax({
        url: server_url + '/ws_report',
        timeout:30000,
        type: "POST",
        data: {client_language: client_language, PIN_code:pin,client_phone:number}
    });
    request.done(function(msg) {
        //alert(JSON.stringify(msg));

    });
    if (msg.ws_resultat.result_ok==true)
    {
        alert('success!');
        window.open("account_details.html");

    }

    request.error(function(jqXHR, textStatus)
    {
       //MESSAGE
    });

}

I tried with <a href="#" onclick="miniReport()"></a>, and also to write the function with $('#idOfHref').click(function(){}); not working.

我试过,并使用$('#idOfHref')编写函数。单击(函数(){});不工作。

All I can see is the alert TEST and then nothing happens. I checked several posts here but nothing works for me.

我能看到的只是警报测试,然后什么都没有发生。我在这里查看了几篇文章,但是没有一篇对我有用。

3 个解决方案

#1


8  

Function can be corrected as,

函数可修正为:

function miniReport(){
    alert('TEST');    
   var client_account_number = localStorage.getItem("numb");

   $.ajax({
        url: server_url + '/ws_report',
        timeout:30000,
        type: "POST",
        data: {"client_language": client_language, "PIN_code":pin,"client_phone":number},
        success : function(msg) {
        //alert(JSON.stringify(msg));
            if (msg.ws_resultat.result_ok == true)
            {
                alert('success!');
                window.open("account_details.html");    
            }
        },
        error: function(jqXHR, textStatus)
        {
           alert('Error Occured'); //MESSAGE
        }
     }
 });

1. No need to assign ajax call to a variable,
2. Your further work should be in Success part of AJAX request, as shown above.

1。不需要为变量2分配ajax调用。您的进一步工作应该是AJAX请求的成功部分,如上所示。

#2


3  

It's a bad practice use an onclick() so the proper way to do this is:

使用onclick()是不好的做法,因此正确的做法是:

Fiddle

小提琴

$(document).ready(function(){
   $('#mylink').on('click', function(){
      alert('onclick is working.');
      miniReport(); //Your function 
   });
});

function miniReport(){
     var client_account_number = localStorage.getItem('numb');
     $.ajax({
        url: server_url + '/ws_report',
        timeout:30000,
        type: "POST",
        data: {
            'client_language': client_language, 
            'PIN_code': pin,
            'client_phone': number
       },
        success: function(msg){
            if (msg.ws_resultat.result_ok==true)
            {
                alert('success!');
                window.open("account_details.html");
            }
        },
            error: function(jqXHR, textStatus)
          {
           //Manage your error.   
          }
    });
}

Also you have some mistakes in your ajax request. So I hope it's helps.

在ajax请求中也有一些错误。所以我希望这能有所帮助。

#3


2  

Rectified version of your code with document .ready

用文档修改代码版本。准备好

    $(document).ready(function(){
    $("#hrefid").click(function(){ // your anchor tag id if not assign any id 

           var client_account_number = localStorage.getItem("numb");
           $.ajax({
            url: server_url + '/ws_report',
            timeout:30000,
            type: "POST",
data:{"client_language":client_language,"PIN_code":pin,"client_phone":number},
            success : function(msg) {

                if (msg.ws_resultat.result_ok == true)
                {
                   window.open("account_details.html");    
                }
                else 
                {
                  alert('some thing went wrong, plz try again');
                }
            }

         }
    });
    });

#1


8  

Function can be corrected as,

函数可修正为:

function miniReport(){
    alert('TEST');    
   var client_account_number = localStorage.getItem("numb");

   $.ajax({
        url: server_url + '/ws_report',
        timeout:30000,
        type: "POST",
        data: {"client_language": client_language, "PIN_code":pin,"client_phone":number},
        success : function(msg) {
        //alert(JSON.stringify(msg));
            if (msg.ws_resultat.result_ok == true)
            {
                alert('success!');
                window.open("account_details.html");    
            }
        },
        error: function(jqXHR, textStatus)
        {
           alert('Error Occured'); //MESSAGE
        }
     }
 });

1. No need to assign ajax call to a variable,
2. Your further work should be in Success part of AJAX request, as shown above.

1。不需要为变量2分配ajax调用。您的进一步工作应该是AJAX请求的成功部分,如上所示。

#2


3  

It's a bad practice use an onclick() so the proper way to do this is:

使用onclick()是不好的做法,因此正确的做法是:

Fiddle

小提琴

$(document).ready(function(){
   $('#mylink').on('click', function(){
      alert('onclick is working.');
      miniReport(); //Your function 
   });
});

function miniReport(){
     var client_account_number = localStorage.getItem('numb');
     $.ajax({
        url: server_url + '/ws_report',
        timeout:30000,
        type: "POST",
        data: {
            'client_language': client_language, 
            'PIN_code': pin,
            'client_phone': number
       },
        success: function(msg){
            if (msg.ws_resultat.result_ok==true)
            {
                alert('success!');
                window.open("account_details.html");
            }
        },
            error: function(jqXHR, textStatus)
          {
           //Manage your error.   
          }
    });
}

Also you have some mistakes in your ajax request. So I hope it's helps.

在ajax请求中也有一些错误。所以我希望这能有所帮助。

#3


2  

Rectified version of your code with document .ready

用文档修改代码版本。准备好

    $(document).ready(function(){
    $("#hrefid").click(function(){ // your anchor tag id if not assign any id 

           var client_account_number = localStorage.getItem("numb");
           $.ajax({
            url: server_url + '/ws_report',
            timeout:30000,
            type: "POST",
data:{"client_language":client_language,"PIN_code":pin,"client_phone":number},
            success : function(msg) {

                if (msg.ws_resultat.result_ok == true)
                {
                   window.open("account_details.html");    
                }
                else 
                {
                  alert('some thing went wrong, plz try again');
                }
            }

         }
    });
    });