【问题标题】:Calling Ajax request function in href在 href 中调用 Ajax 请求函数
【发布时间】:2018-05-17 17:22:18
【问题描述】:

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

当点击 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
    });

}

我尝试使用<a href="#" onclick="miniReport()"></a>,也尝试使用$('#idOfHref').click(function(){}); 编写函数不起作用。

我只能看到警报 TEST,然后什么也没有发生。我在这里查看了几篇帖子,但没有什么对我有用。

【问题讨论】:

  • $.ajax({ ...替换var request = $.ajax({ ...
  • 将您的if (msg.ws_resultat.result_ok==true) 放入 done 回调中。

标签: javascript jquery ajax html


【解决方案1】:

函数可以修正为,

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。无需将 ajax 调用分配给变量,
2. 你进一步的工作应该在AJAX请求的成功部分,如上图所示。

【讨论】:

  • 神秘变量request是什么?
  • 2.您不需要引用对象中的键。
  • @jcubic,是的。刚刚通过stringify
【解决方案2】:

使用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.   
          }
    });
}

您的 ajax 请求也有一些错误。所以我希望它会有所帮助。

【讨论】:

    【解决方案3】:

    您的代码的修正版本,带有文档 .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');
                    }
                }
    
             }
        });
        });
    

    【讨论】:

      猜你喜欢
      • 2018-12-25
      • 1970-01-01
      • 2020-05-09
      • 2017-08-12
      • 2020-02-29
      • 2011-10-13
      • 2011-09-27
      • 1970-01-01
      • 2016-06-28
      相关资源
      最近更新 更多