javascript - POST 适用于 JQuery,但不适用于 XMLHttpRequest
问题描述
所以我试图从 Javascript 发布到我的服务器(php),并试图不使用 JQuery。
此代码有效并将必要的数据发布到数据库
var msg = {};
msg['name'] = 'joe';
msg['message'] = 'why no work';
$.post(phpURL, msg, function(data) {});
但是这个没有
var xhr = new XMLHttpRequest();
xhr.open("POST", phpURL, true);
xhr.send(msg);
我什至查看了我的 php 日志,查看了标题,我能看到的 JQuery 与 XHR 的唯一区别是 content-type header"application/x-www-form-urlencoded; charset=UTF-8"
和 this header "x-requested-with" "XMLHttpRequest"
。
所以我尝试了以下标题的所有组合。
var xhr = new XMLHttpRequest();
xhr.open("POST", phpURL, true);
//xhr.setRequestHeader('Content-Type', 'application/json');
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
//xhr.setRequestHeader('x-requested-with', 'XMLHttpRequest');
xhr.send(msg);
没有效果。
值得一提的是,如果我尝试在JSON.stringify(msg)
任何地方添加,它不起作用,无论是在 JQuery 还是 XHR 中。但我想先让这个工作,并解释这个奇怪的差异。
我倾向于认为这是一个 Javascript 问题,因为 JQuery 帖子有效,此外,服务器的 GET 请求和我尝试发布的同一张表也有效。
解决方案
如果你将一个对象传递给 jQuery 的data
参数,那么它将把它编码为application/x-www-form-urlencoded
数据(不是 JSON!)。
如果您application/x-www-form-urlencoded
将数据发布到 PHP,那么它将解析它并$_POST
用它填充超全局。
如果您将对象传递给对象的send()
方法,XMLHttpRequest
那么它将不会为您编码。它会隐式调用.toString()
它,并且根本不会发送任何有用的东西。
要达到与 jQuery 相同的效果,您需要自己对数据进行编码。不要忘记也设置Content-Type
标题!
const encoded = new URLSearchParams(Object.entries(msg)).toString();
const xhr = new XMLHttpRequest();
xhr.open("POST", phpURL, true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send(encoded);
如果要发送 JSON,则还必须对其进行编码,但这很简单,JSON.stringify()
尽管您还需要设置 Content-Type 标头(application/json
此时)。
const encoded = JSON.stringify(msg);
const xhr = new XMLHttpRequest();
xhr.open("POST", phpURL, true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.send(encoded);
但是,PHP不会自动解析 JSON ,所以$_POST
会一直为空,所以需要手动解析。
<?php
$json = file_get_contents('php://input');
$msg = json_decode($json);
推荐阅读
- javascript - Laravel+Vuejs:v-model 没有连接到 app.js
- sapui5 - sap.m.Wizard:discardProgress 后 nextStep 无法正常工作
- ruby - Ruby 动态方法调用的问题
- reactjs - 当我尝试将道具传递给孩子时映射不是函数错误
- dbeaver - 有没有办法找到引用 DBeaver 中另一个表/列的所有表?
- sql-server - SQL SERVER:子查询中的 WHERE 子句从外部查询中获取属性
- firebase - Firebase 身份验证和谷歌云翻译
- r - R 函数仅显示列的 20% 最高值
- laravel - 如何使用两个输入文本进行搜索,一个用于搜索,另一个用于排除
- sql - MS Access 查询输入必须包含至少一个表或查询