doujizhong8352 2012-11-24 20:40
浏览 23

使用jQuery和PHP发送和接收AJAX请求

I'm trying to send an AJAX request to a page to interpret some data, and if it complies with what i'm looking for, send another AJAX request back. Right now I can see the first request is being made, but I'm not getting one back

//message.php
<head>
<script>
function org_name(str){
    alert(str); //this alert appears, so I know the data is being received by this function
    $.get("common_functions.php", { group_name: str} );
}
</script>
</head>    

Then, on common_functions.php I have a similar request back, however, I'm not sure exactly what the issue is. The alert box doesn't even appear so I'm confused as to why the console would say the request was sent

//common_functions.php

if(isset($_GET['group_name'])){
  ?>
  <script src="http://code.jquery.com/jquery-1.8.2.js"></script>
  <script type="text/javascript">
  alert('common_functions'); //this does not appear
  $.get("message.php", { name: "test"} );
  </script>
  <?
}

When I open up the javascript console in chrome I see the request sent form message to common_functions, but apparently the request on common_functions isn't sending one back

//text from javascript console
Request URL:http://localhost/message/common_functions.php?group_name=test
Request Method:GET
Status Code:200 OK

Does anyone see something obvious that I'm doing wrong or missing? If it makes a difference, common_functions is included in message.php because I do use some other functions from that page for my php.

  • 写回答

4条回答 默认 最新

  • doujinai2183 2012-11-24 20:44
    关注

    $.get will strip out <script> tags. You can use another jQuery AJAX method load() that won't, or use $.getScript. If you need content and script you can do both by making an ajax request for the content, and in the success callback of that ajax, call for the script with $.getScript

    load() replaces all the content in the specified selector

    $('#myDiv').load('common_functions.php', { name: "test"})  
    

    Will take all the content of the php output and replace all the contents of #myDiv but will also run the scripts

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!