Почтальон работает, а Фетч - нет. Что не так? - PullRequest
0 голосов
/ 13 мая 2019

У меня проблемы с получением информации из JSON.Это мой код:

    fetch('http://fruitmruit.com/webservice/products/details', {
      method: 'POST',
      headers: {
        Accept: 'application/json',
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({
        product : '145',
      }),
    }).then((response) => response.json())
    .then((responseJson) => {
      console.log(responseJson)
      this.setState({
        loading: false,

      }, function(){

      });
    })
    .catch((error) => {
      console.error(error);
    });

}

И проблема в том, что я получаю status : "error" в console.log ():

enter image description here

Но в Почтальоне получите status : "ok":

enter image description here

Может быть, проблема заключается в "http" в URL?Спасибо, что прочитали мой вопрос.

1 Ответ

1 голос
/ 13 мая 2019

Поскольку тип тела принимается как x-www-form-urlencoded. Введите тип содержимого

application/x-www-form-urlencoded

и тело как простую строку, чтобы функция выборки стала такой, как показано ниже

test(){
    console.log('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.');
    fetch('http://fruitmruit.com/webservice/products/details', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
      },
      body: 
        'product=145'
      ,
    }).then((response) => response.json())
    .then((responseJson) => {
      console.log('response>>>>>>>>>>>>>>>>>'+ JSON.stringify(responseJson))

    })
    .catch((error) => {
      console.error('>>>>>>>>>>>>>>>'+error);
    });

}
...