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
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能