3

I am trying to do fetch a data from the server with given datas. I am using the following code :

    const details = {
      'function': 'getUsers',
    };
    const formBody = 
    Object.keys(details).map(key=>encodeURIComponent(key)+
    '='+encodeURIComponent(details[key])).join('&');
    console.log(formBody);
    fetch(url, {
      method: 'POST',
      credentials: 'include',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
        'Cookie': 'XPIDER_SID=' + this.props.text
      },
      body: formBody
      }).then((response) => response.json())
        .then((responseJson) => {
          if (responseJson.status === 'okay') {
            this.setState({ users: responseJson.users });
          } else {
             this.setState({ error: responseJson.error });
          }
      })

It works great until now. Now I have to send a array of datas but when I write an array in details constant, the server does not take the array because of the formBody. In formBody, the whole request is string, so the array converted to a string. How can I send a array with this ? or do you guys know any other option ? Thank You !

1
  • I think you can use a data parameter in the request Commented Nov 20, 2017 at 10:51

1 Answer 1

6

Send your array as JSON like this:

body: JSON.stringify(your_array)

Then deserialize it on the server

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the answer. But I cant send a JSON because the format must be x-www-form-urlencoded

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.