tags:

views:

609

answers:

2

I have a jQuery AJAX call with type:'GET' like this:

$.ajax({type:'GET',url:'/createUser',data:"userId=12345&userName=test",
   success:function(data){
     alert('successful');
   }
 });

In my console output is: GET:http://sample.com/createUser?userId=12345&userName=test params: userId 12345 userName test

In my script i should get the value using $_GET['userId'] and $_GET['userName'] but i can't get the value passed in by ajax request using GET method.

Any ideas on how to do this?

thanks,

A: 

The url is missing the extension, and you can pass data as an object, so jquery convert it to a string

$.ajax({
  type:'GET',
  url:'/createUser.php',
  data:{"userId":"12345","userName":"test"},
  success:function(data){
    alert('successful');
  }
);
URL's can't "miss the extension" they are URLs not filenames. Also, the jQuery Ajax method explicitly accepts a string: """It is converted to a query string, if not already a string."""
David Dorward
+1  A: 

The only thing I can see wrong with the code (which no longer applies as the question has been edited (which suggests that the code has been rewritten for the question and might not accurately reflect the actual code in use)) is that the success function is in the wrong place.

You have:

$.ajax(
   {
      type:'GET',
      url:'/createUser',
      data:"userId=12345&userName=test"
   },
   success: function(data){
     alert('successful');
   }
);

which should be:

$.ajax(
   {
      type:'GET',
      url:'/createUser',
      data:"userId=12345&userName=test",
      success: function(data){
        alert('successful');
      }
   }
);

Despite this, your description of the console output suggests the data is being sent correctly. I'd try testing with this script to see what PHP actually returns (you can see the body of the response in the Firebug console):

<?php
    header("Content-type: text/plain");
    print_r($_REQUEST);
?>
David Dorward
sorry for the mistake, yeah it should like what you suggested.
Trez