doupu7651 2019-05-03 19:46
浏览 105

JS json在PHP上收到解密错误

I sent a JSON string from Javascript and received it on PHP, however my PHP script is not receiving it as JSON.

The JS generates this output:

{"id_own":"Metztli Alonso","tick":"123456","ticket":"TID","comm":"test viernes nnuevo formato php","NQueues":"AMX-GI-Administracion-Interna","NCauses":"Capacitacion"}

But the PHP receives this:

id_own=Metztli+Alonso&tick=123456&ticket=TID&NQueues=AMX-GI-Administracion-Interna&NCauses=Capacitacion&comm=test+viernes+nnuevo+formato+php

JS

//var obj = { id_own: f.id_own.value, tick: f.Ticket.value, ticket: TTYPE, comm: f.comments.value, NQueues: f.que.value, NCauses: f.cau.value };

//Sample data
var obj = { id_own: "Metztli Alonso", tick: "123456", ticket: "TID", comm: "test viernes nnuevo formato php", NQueues: "AMX-GI-Administracion-Interna", NCauses: "Capacitacion" };

const myJSON = JSON.stringify(obj);

alert(myJSON);

const xhr = new XMLHttpRequest();
xhr.open("POST","http://*****/dev_insert.php");
xhr.setRequestHeader("Content-Type","application/json");
xhr.send(myJSON);

PHP

<?php
echo "<br>";
echo "mensaje de js";
$requestPayload = file_get_contents("php://input");
var_dump ($requestPayload);
</div>
  • 写回答

1条回答 默认 最新

  • douzhongqiu5032 2019-05-06 17:22
    关注

    The code you've provided, as it, won't do that.

    The symptoms that you describe indicate that you are collecting the data from a form and running the JavaScript when the form is submitted, but not cancelling the default behaviour of the submit event.

    Consequently:

    1. The submit event fires
    2. The JavaScript runs and initiates the Ajax request
    3. The JavaScript finishes
    4. The form submits … and URL encodes the data in it as you describe
    5. The Ajax request is cancelled because the environment it is running in is going away
    6. The browser renders the response to the form request

    You need to prevent the default behaviour of the form submission:

    form.addEventListener("submit", event => {
        event.preventDefault();
        // Then do everything you were already doing
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何实验stm32主通道和互补通道独立输出
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题