最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery - Ajax submit JSON data in GET - Stack Overflow

programmeradmin1浏览0评论

Here is what I've tried:

JS:

$.ajax({
    type: 'GET',
    contentType: 'application/json; charset=UTF-8',
    data: JSON.stringify( { data: 'bla' } ),
    dataType: 'json',
    success: function( data ) {
        alert(data);
    },
    error: function( data ) {
        alert('error: ' + data);
    }
});

PHP:

<?php

    if ( $_SERVER['REQUEST_METHOD'] == 'GET' )
    {
        $data = json_decode( file_get_contents('php://input') );

        echo json_encode( $data );
    }

?>

Output:

null

If you swap the GET with a POST instead it works like a charm. "Why not just use the POST instead?", I use POST to perform inserts into the database. I have an inkling feeling it might be because it forcefully wants the GET data in this kind of format: [email protected]&firstname=Bob&lastname=Mctest. I don't know why though because the content type is set as application/json, this is somewhat perplexing. Just to add something here from my last edit, the php://input is empty in other words it's not sending any data to it but it still works without problems using curl GET from the terminal.

When I run a curl GET mand from the terminal using the JSON content type it works like a charm and spits out the data without issue i.e. curl -v -X GET -H "Content-Type: application/json" -d '{"data":"bla"}' /test.php

I'm not sure how helpful this will be but I found a PHP Fiddle site and pasted the example code in there just to make it easier for everyone, here is the link:

Thank you very much for any help.


Edit: Fixed the spell error in 'application/json' pointed out by SilverBlade and updated the PHP Fiddle link (null output still persists in other words still not working). The problem, which I should have stated above, is the php://input is actually empty there is no data in there at all when posted via jQuery - no issues when used in-conjunction with curl GET.


Edit2: I'm not using IIS, thank you to robertdeniro (heh) for pointing it out.

Here is what I've tried:

JS:

$.ajax({
    type: 'GET',
    contentType: 'application/json; charset=UTF-8',
    data: JSON.stringify( { data: 'bla' } ),
    dataType: 'json',
    success: function( data ) {
        alert(data);
    },
    error: function( data ) {
        alert('error: ' + data);
    }
});

PHP:

<?php

    if ( $_SERVER['REQUEST_METHOD'] == 'GET' )
    {
        $data = json_decode( file_get_contents('php://input') );

        echo json_encode( $data );
    }

?>

Output:

null

If you swap the GET with a POST instead it works like a charm. "Why not just use the POST instead?", I use POST to perform inserts into the database. I have an inkling feeling it might be because it forcefully wants the GET data in this kind of format: [email protected]&firstname=Bob&lastname=Mctest. I don't know why though because the content type is set as application/json, this is somewhat perplexing. Just to add something here from my last edit, the php://input is empty in other words it's not sending any data to it but it still works without problems using curl GET from the terminal.

When I run a curl GET mand from the terminal using the JSON content type it works like a charm and spits out the data without issue i.e. curl -v -X GET -H "Content-Type: application/json" -d '{"data":"bla"}' http://codes.local/test.php

I'm not sure how helpful this will be but I found a PHP Fiddle site and pasted the example code in there just to make it easier for everyone, here is the link: http://phpfiddle/main/code/u4b-xkh

Thank you very much for any help.


Edit: Fixed the spell error in 'application/json' pointed out by SilverBlade and updated the PHP Fiddle link (null output still persists in other words still not working). The problem, which I should have stated above, is the php://input is actually empty there is no data in there at all when posted via jQuery - no issues when used in-conjunction with curl GET.


Edit2: I'm not using IIS, thank you to robertdeniro (heh) for pointing it out.

Share Improve this question edited Sep 28, 2013 at 0:42 adamj asked Sep 27, 2013 at 1:46 adamjadamj 4,7926 gold badges51 silver badges60 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 2

My suspicions were accurate and the cause of the problem was jQuery $.ajax/$.get was forcing query strings when using GET even when you've set the content type as JSON, makes no sense but I will have to dissect the manuals to find out why that is.

Here is my working code with a workaround that works with curl GET and $.ajax/$.get.

JS:

$.ajax({
    type: 'GET',
    url: 'manage.php',
    data: 'user=bla',
    dataType: 'json',
    success: function( data ) {
        // code here...
    }
});

PHP:

$get = !empty( $_GET ) ? json_decode( json_encode( $_GET ) ) : json_decode( file_get_contents('php://input') );

SilverBlade may have found your issue, but there is still a munication issue between XHR and PHP. PHP is required to return the correct headers or else strange things can happen with jQuery interpretation.

Try:

header('Content-type: application/json');

Your content type has a spell error,

contentType: 'appllication/json; charset=UTF-8',

It should be

contentType: 'application/json; charset=UTF-8',

Secondly, you may check with specifying the URL.

Use $.get & $.post instead of $.ajax, life would be much easier. Like this:

$.get(request_url, { param: value }, function(data){
  // callback here
}, 'json')

I don't understand what you mean that you can't use a POST because you're using POST for database inserts.
POST is for sending data to the server & probably why the server is ignoring data in the GET - because it's not required to process data in a GET.
If you are already using POST then you can extend it - like your json could have 2 properties - Action & Data.
Action = [dbInsert, dbSomethingElse, fileWrite, etc]
Data would be the raw data.
You would take different code paths based on Action
I don't know what web server you're using but IIS ignores data in a GET - regardless of 'application/json; charset=UTF-8', 'application/x-www-form-urlencoded', etc.

发布评论

评论列表(0)

  1. 暂无评论