【问题标题】:Using HTTP in NativeScript to send Post-data to a TYPO3-Webservice在 NativeScript 中使用 HTTP 将 Post-data 发送到 TYPO3-Webservice
【发布时间】:2021-01-30 02:21:00
【问题描述】:

我正在尝试将表单数据从 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 正在被填充。所以我猜在调用控制器之前会丢失一些东西。

有什么想法吗?

【问题讨论】:

    标签: javascript nativescript typo3-8.x


    【解决方案1】:

    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}`);
    });
    

    【讨论】:

    • 谢谢。您使用什么服务器端代码?什么框架?
    猜你喜欢
    • 2013-10-12
    • 1970-01-01
    • 2017-12-11
    • 2014-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多