开发者

jQuery AJAX type: 'GET', passing value problem

开发者 https://www.devze.com 2022-12-22 13:48 出处:网络
I have a jQuery AJAX call with type:\'GET\' like this: $.ajax({type:\'GET\',url:\'/createUser\',data:\"userId=12345&userName=test\",

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,


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);
?>


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');
  }
);
0

精彩评论

暂无评论...
验证码 换一张
取 消