问题描述
我正在尝试将表单数据从 NativeScript 应用程序发送到 TYPO3-Webservice。
这是我正在使用的 JavaScript:
httpModule.request({
url: "https://my.domain.tld/webservice?action=login",method: "POST",headers: { "Content-Type": "application/json" },content: JSON.stringify({
username:username,password:password
})
}).then((response) => {
console.log("got response");
console.log(response.content);
//result = response.content.toJSON();
callback(response.content.toJSON());
},(e) => {
console.log("error");
console.log(e);
});
但是我无法在控制器中读取这些数据。即使这样:
$rest_json = file_get_contents("PHP://input");
$postvars = json_decode($rest_json,true);
$postvars
为空。 $_POST
也是空的(根据一些文档,这是因为数据以 JSON 形式发送,因此未填充 $_POST
-Array。
无论我做什么,无论我尝试什么,我都无法将这些变量输入到我的控制器中。
我用 fetch
和 formData
而不是 JSON.stringify
尝试过,结果相同。
我可能需要补充一点,当我在 TYPO3 的 index.PHP 中添加 PHP 部分时,会填充 $postvars
。所以我想有些东西会丢失,直到控制器被调用。
有什么想法吗?
解决方法
nativescript 部分看起来没问题,您的问题必须在服务器端更正。
我使用类似的调用及其作品
// send POST request
httpModule.request({
method: "POST",url: appSettings.getString("SERVER") + '/product/list',content: JSON.stringify(data),headers: {"Content-Type": "application/json"},timeout: 5000,}).then(response => { // handle replay
const responseAsJson = response.content.toJSON();
console.log('dispatchAsync\n\tresponse:',responseAsJson);
},reason => {
console.error(`[ERROR] httpModule,msg: ${reason.message}`);
});