的contentType:“应用/ JSON的”不工作的JSON字符串? [PHP]字符串、工作、contentType、JSON

2023-09-11 01:40:51 作者:Ju2て |▍最后旳挽歌

我是新来的JSON和尝试了一些测试传递一个PHP对象到另一个网站.. 应用/ JSON的的字符串无法通过,照片:如何当我使用的的contentType它来 但是,当IM unsing默认的的contentType:应用程序/ x-WWW的形式urlen codeD 的传递 字符串和去codeS就没有问题?

或者,传递一个对象,但不是一个字符串,当您使用的应用程序/ JSON类型而已? 也许我只是失去了一些东西根本在这里..

PHP OBJ

  $ _对象=(对象)['名称'=> 在这里,我们走!,luckyNum'=> 6,rndText'=> jkfid'];
 

JS

  $('#BTN1')。在('点击',函数(){
VAR json1 = JSON.stringify(小于?PHP的回声json_en code($ _对象)>);
    $阿贾克斯({
        网址:try.php,
        键入:POST,
        的contentType:应用/ JSON的;字符集= UTF-8,
        数据 : {
            json1:json1
        }
    })
    .done(函数(响应){执行console.log(响应);})
    .fail(功能(jqXHR,textStatus,errorThrown){警报([错误'+ jqXHR.status +]+ textStatus +:+ errorThrown);});
});
 

try.php

 < PHP
标题(内容类型:应用程序/ JSON;字符集= UTF8');
$ json1 = json_de code($ _ POST [json1]);

回声播放器。 $ json1->名称;
回声\ NHIS幸运数字是:。 $ json1-> luckyNum;

?>
 
前后端交互

解决方案

您POST请求不是JSON字符串,它是封装在一个应用程序/ x-WWW的形式JSON字符串-urlen codeD 消息。

或者:

发送有实际纯JSON消息

传递传递对象的JSON字符串数据代替。传递对象将jQuery的EN code的内容的表单数据。

 数据:json1
 

和修改PHP的,因此停止尝试读取应用程序/ x-WWW的形式urlen codeD 数据:

  $ inputJSON =的file_get_contents('php的://输入');
$输入= json_de code($ inputJSON,TRUE);
 

停止声称要发送的JSON

删除:

 的contentType:应用/ JSON的;字符集= UTF-8,
 

I'm new to JSON and tried some testing for passing an PHP Object to another site.. How does it come when I'm using contentType : 'application/json' the string can't be passed, but when im unsing the default contentType : 'application/x-www-form-urlencoded' it passes the string and decodes it without problems?

Or do you use the application/json type only when passing an object but not a string? Maybe I'm just missing something fundamental here..

PHP OBJ

$objectX = (object) ['name' => 'Here we go!','luckyNum' => 6,'rndText' => 'jkfid'];  

JS

$('#btn1').on('click' , function(){
var json1 = JSON.stringify(<?php echo json_encode($objectX) ?>);    
    $.ajax({
        url : 'try.php',
        type : 'POST',
        contentType : 'application/json; charset=UTF-8',
        data : {
            json1 : json1
        }
    })
    .done (function(response) { console.log(response); })
    .fail (function(jqXHR, textStatus, errorThrown ) {  alert('[Error ' + jqXHR.status + "] "  + textStatus + " : " + errorThrown); });
});

try.php

<?php
header('Content-Type: application/json; charset=UTF8');
$json1 = json_decode($_POST["json1"]);

echo "Player is: " . $json1->name;
echo "\nHis lucky Number is: " . $json1->luckyNum;

?>

解决方案

Your POST request isn't "a JSON string", it is a JSON string encapsulated in a application/x-www-form-urlencoded message.

Either:

Send an actual plain JSON message

Pass the JSON string to data instead of passing an object. Passing an object will make jQuery encode the content as form data.

data : json1

and change the PHP so it stops trying to read application/x-www-form-urlencoded data:

$inputJSON = file_get_contents('php://input');
$input= json_decode( $inputJSON, TRUE );

Stop claiming you are sending JSON

Remove:

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

 
精彩推荐