weixin_33720078 2017-02-05 19:10 采纳率: 0%
浏览 45

反应ajax请求

Can somebody help me with ajax request to server? I'm trying to send data to server

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

Function with ajax request

But this function doesn't work properly

Here jquery ajax request and he is works like expected

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

any ways to make it work?

Thanks a lot!

  • 写回答

1条回答 默认 最新

  • weixin_33736649 2017-02-05 19:14
    关注

    Your jQuery version is encoding the data as x-www-form-urlencoded but your fetch version is encoding the data as JSON.

    You need to either continue to encode the data using the x-www-form-urlencoded format, or change your server side code to expect JSON.

    评论

报告相同问题?