简体   繁体   English

PHP 没有收到来自 javascript 发送的 json 数据

[英]PHP didn't receive json data from javascript send

I try to send a request from javascript with the code below, but the problem is that php server didn't receive data in $POST or $_GET.我尝试使用下面的代码从 javascript 发送请求,但问题是 php 服务器没有在 $POST 或 $_GET 中接收数据。 I tried many solutions but i didn't find one.我尝试了很多解决方案,但没有找到。

request: function(query, type = "POST", data, async = false, callback) {请求:函数(查询,类型=“POST”,数据,异步=假,回调){

        var xhttp = new XMLHttpRequest();
        var args = [];

        if(arguments.length > 4)
            for (var i = 5; i < arguments.length; i++)
                args.push(arguments[i]);

        xhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
                if(async) {
                    msg = this.responseText;
                    console.log(msg);
                    msg = JSON.parse(msg);
                    //Context.update(msg.extended);
                    var array = [msg];
                    for (var i = 0; i < args.length; i++)
                        array.push(args[i]);
                    console.log(args);
                    callback.apply(this,array);
                } else {
                    msg = this.responseText;
                    console.log(msg);
                    msg = JSON.parse(msg);
                    console.log(msg);
                }
            }
        };

        xhttp.open(type, query, async);
        xhttp.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');

        if(Object.keys(data).length > 0)
            xhttp.send(JSON.stringify(data));
        else
            xhttp.send();
        if(typeof(msg) !== "undefined")
            return msg;


    }

Maybe use an existing solution to make request, like fetch .也许使用现有的解决方案来发出请求,比如fetch

It will help you to no lose time on badly rewriting existing solution and focus on your real application goal.它将帮助您不浪费时间重新编写现有的解决方案,并专注于您真正的应用程序目标。

Example code from the doc文档中的示例代码

async function postData(url = '', data = {}) {
  // Default options are marked with *
  const response = await fetch(url, {
    method: 'POST', // *GET, POST, PUT, DELETE, etc.
    mode: 'cors', // no-cors, *cors, same-origin
    cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
    credentials: 'same-origin', // include, *same-origin, omit
    headers: {
      'Content-Type': 'application/json'
      // 'Content-Type': 'application/x-www-form-urlencoded',
    },
    redirect: 'follow', // manual, *follow, error
    referrerPolicy: 'no-referrer', // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
    body: JSON.stringify(data) // body data type must match "Content-Type" header
  });
  return response.json(); // parses JSON response into native JavaScript objects
}

postData('https://example.com/answer', { answer: 42 })
  .then(data => {
    console.log(data); // JSON data parsed by `data.json()` call
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM