如何通过 php 脚本在 Ajax 中发送响应

我需要从 ajax 向 php 代码发送响应。成功时要显示的警报消息。我必须输入日期,该日期应显示在要发送的 url 和响应中。


  <script>  

  $(document).ready(function() {

$('#txtdate').change(function(){

    date = $(this).val();

  $.ajax({ 

                 type: 'GET',   

                 url: "http://localhost/data/check_date.php?date=" +date , 

                 success: function() {

                  alert(data);


                           }

           });



});

  });

</script>


梵蒂冈之花
浏览 103回答 3
3回答

拉莫斯之舞

var date = $(this).val();$.ajax({&nbsp;&nbsp; &nbsp;type: 'GET',&nbsp; &nbsp;&nbsp; &nbsp;url: "http://localhost/data/check_date.php?date=" +date ,&nbsp;&nbsp; &nbsp;dataType: 'text',&nbsp; &nbsp;success: function(data) { //add data in the function which is returned from the file&nbsp; &nbsp; alert(data);&nbsp; &nbsp;}});还可以尝试在执行 ajax 请求之前通过在浏览器中单击来打开网络选项卡F12,如果您的 php 文件中存在错误,您可以在网络选项卡中查看它,看看是否有帮助

GCT1015

两个错误:1:date&nbsp;=&nbsp;$(this).val();到var&nbsp;date&nbsp;=&nbsp;$(this).val();2:url:&nbsp;"http://localhost/data/check_date.php?date="&nbsp;+date&nbsp;",到url:&nbsp;"http://localhost/data/check_date.php?date="&nbsp;+date&nbsp;,3:success:&nbsp;function()&nbsp;{到success:&nbsp;function(data)&nbsp;{所有脚本:<script>&nbsp;&nbsp;&nbsp; $(document).ready(function() {$('#txtdate').change(function(){&nbsp; &nbsp; var date = $(this).val();&nbsp; $.ajax({&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;type: 'GET',&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;url: "http://localhost/data/check_date.php?date=" +date ,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;success: function(data) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert(data);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;});});&nbsp; });</script>

一只斗牛犬

删除"在url末尾提到的,因为date是要通过url传递的变量。要克服所有错误,请将整个代码更改为<script>&nbsp;&nbsp;&nbsp; $(document).ready(function() {$('#txtdate').change(function(){&nbsp; &nbsp; var date = $(this).val();&nbsp; $.ajax({&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;type: 'GET',&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;url: "http://localhost/data/check_date.php?date=" +date,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;success: function(date) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert(date);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; });</script>
打开App,查看更多内容
随时随地看视频慕课网APP