使用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.

$.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

You have to do something with your data. Right now, you're making an AJAX call, and doing nothing with your data.

So something like the following would work:

$.ajax({
    url: "common_functions.php",
    data: { group_name: str },
    type: "GET",
    success: function (data) {
        $(data).appendTo("head");
    }
});

Use $.ajax if you want control over execution states:

        $.ajax({
            type: "POST",
            url: "common_functions.php",
            data: { name: "test"},
            success: function(r)
            {
                 // user r as output
            },
            error: function(xhr, ajaxOptions, thrownError)
            {
                 // error report
                alert(xhr.responseText);
            }
    });

In this case you can see if execution was successful or if error occurred.

Also you can use firebug add-on for firefox or chrome to detect if response was sent. There is also an excellent tool called Fidler, which can give you much better overview over request/response states.

Here is an excellent tutorial for ajax debugging.

you may use a library that does that for you automatically, using http://phery-php-ajax.net in your case, it would be

function messages($data){
  $r = new PheryResponse;
  // return your messages
  return $r->json($messages);
}

function common($data){
  $r = new PheryResponse;
  switch ($data['group_name']){
    case 'messages':
       $r
       ->include_script(array( 
         'jquery' => 'http://code.jquery.com/jquery-1.8.2.js'
       ))
       ->phery_remote('messages'); // load your messages
       break;
  }
  return $r;
}

Phery::instance()->set(array(
  'common' => 'common',
  'messages' => 'messages'
))->process();

on your page load

$(function(){
  phery.remote('common', {'group_name': 'messages'});
});

you don't have to do anything else. btw, if you are including jQuery AFTER you are using $.get(), it won't work, obviously