繁体   English   中英

反应ajax请求

[英]React ajax request

有人可以帮我处理对服务器的Ajax请求吗? 我正在尝试将数据发送到服务器

static language(lang) {

    const request = new Request("/change-language", {
        method: 'POST',
        body: JSON.stringify({lang: lang})
    });

    return fetch(request).then(response => {
        return response.json();
    }).catch(error => {
        return error;
    });
}

带ajax请求的功能

但是此功能无法正常工作

在这里,jQuery ajax请求和他的工作像预期的那样

$.ajax({
  type: 'POST',
  url: '/change-language',
  data: {lang: 'esp' },
success: function(data){
  console.log(data)
 }
});

有什么方法可以使其工作?

非常感谢!

您的jQuery版本将数据编码为x-www-form-urlencoded,但是您的提取版本将数据编码为JSON。

您需要继续使用x-www-form-urlencoded格式对数据进行编码,或者更改服务器端代码以使用JSON。

暂无
暂无

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

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