【问题标题】:AJAX POST method isn't working in androidAJAX POST方法在android中不起作用
【发布时间】:2018-11-11 00:36:59
【问题描述】:

我的表单应该使用 AJAX 发布,我拥有的所有其他设备都可以发布,除了 android。我试过改变获取方法。我什至有条件查找设备是否是 android,但我希望能够向 android 用户提供 AJAX。如果我需要提供更多信息,如果有人处理过类似的事情,我也会非常高兴。

html:

      <form method='post' action='insert.php' id="requestForm">
           <input class="ifields" type="text" id="song_name" name="song_title" placeholder="Song">
           <input class="ifields" type="text" id="song_author" name="song_artist" placeholder="Artist">
           <input class="ifields" type="text" id="song_singer" name="users_name" placeholder="Your Name">
           <br><br><button type="button" name="btn_add" id="btn_add" class="btn btn-xs btn-success isubmit">Submit</button>
      </form>

jquery:

                 $(document).on('click', '#btn_add', function(e){  
                      var song_name = $('#song_name').val();  
                      var artist_name = $('#song_author').val();  
                       var user_input_name = $('#song_singer').val();
                      if(song_name == '')  
                      {  
                           alert("Enter song Name");  
                           return false;  
                      }  
                      if(artist_name == '')  
                      {  
                           alert("Enter artist Name");  
                           return false;  
                      } 
                      if(user_input_name == '')  
                      {  
                           alert("Enter your name Name");  
                           return false;  
                      }   
                     var ua = navigator.userAgent.toLowerCase();
                     var isAndroid = ua.indexOf("android") > -1; //&& ua.indexOf("mobile");
                     if(isAndroid) {
                          $.ajax({  
                               url:"insert_admin.php",  
                               method:"POST",  
                               data:{song_name:song_name, artist_name:artist_name, user_input_name:user_input_name},  
                               dataType:"text",  
                               success:function(data)  
                               {  
                                    alert("your song was added");  
                                    fetch_data();
                                    $("form")[0].reset();  
                               }  
                          })  
                     }
                     else{
                          e.preventDefault();
                          $.ajax({  
                               url:"insert_admin.php",  
                               method:"POST",  
                               data:{song_name:song_name, artist_name:artist_name, user_input_name:user_input_name},  
                               dataType:"text",  
                               success:function(data)  
                               {  
                                    alert("your song was added");  
                                    fetch_data();
                                    $("form")[0].reset();  
                               }  
                          })  
                     }   
                 }); 

【问题讨论】:

  • 你是如何以及何时调用 ajax 函数的。
  • @MohammadC 我刚刚更新了脚本
  • 谢谢。试试我的解决方案,让我知道结果如何。

标签: jquery ajax forms post


【解决方案1】:
$("btn_add").click(function(e){  
    var song_name = $('#song_name').val();  
    var artist_name = $('#song_author').val();  
    var user_input_name = $('#song_singer').val();
    if(song_name == '')  
    {  
        alert("Enter song Name");  
        return false;  
    }  
    if(artist_name == '')  
    {  
        alert("Enter artist Name");  
        return false;  
    } 
    if(user_input_name == '')  
    {  
        alert("Enter your name Name");  
        return false;  
    }   
    $.ajax({  
        url:"insert_admin.php",  
        method:"POST",  
        data:{"song_name":song_name, "artist_name":artist_name, "user_input_name":user_input_name},  
        dataType:"text",  
        success:function(data)  
        {  
            alert("your song was added");  
            fetch_data();
            $("#requestForm").reset();  
        }  
    });
}); 

嘿,伙计。试试这个,让我知道它是否有效。这应该希望只在按钮上创建一个单击事件侦听器。先在电脑上试一下,检查控制台日志是否有任何错误。如果没有错误。然后继续在安卓手机上试用。手指交叉,没有问题。

【讨论】:

  • 谢谢你!我通过几个机器人运行它并在我的应用程序上运行。我永远不会得到报价。你知道为什么我必须在某些情况下将状态放在引号中,而在其他情况下不需要? @MohammadC
  • 您需要用引号来命名需要为字符串的字段。目前您正在动态命名字段并动态设置数据,后者很好。如果您动态设置字段名称,您将如何知道字段名称是什么。例如,有人输入 test 作为歌曲名称。您正在发送 {"test":"test"}。您想发送 {"song_name":"test"} 以便您可以通过引用 song_name 来获取歌曲名称,我希望这是有道理的。我也很困惑为什么它在某些情况下有效,而不是在其他情况下,尤其是 android。也许 PC 浏览器的工作方式略有不同。
猜你喜欢
  • 2012-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-21
  • 2019-09-17
  • 2021-01-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多