0

Axios get call receiving the right information but it comes back as a string of the XML data. I need a JSON object.

I am grabbing all the tables on the page then I filter through them and then I want to make a REST call that will only give me the Choice fields. The variable current list name gets the guid and that is later concat'd for the url.


        var currentListName = table.getAttribute("id").substring(1, 37);
        var root = ctx.HttpRoot;
        var listName = "SP.Data." + table.summary + "ListItem";
        var data = {
          __metadata: { type: listName },
        };
        var url =
          root +
          "/_api/web/lists('" +
          currentListName +
          `')/fields?$filter=TypeDisplayName eq 'Choice'`;
        var configureAxios = {
          headers: {
            Accept: "application/json;odata=verbose",
            "Content-Type": "application/json;odata=verbose",
            credentials: true,
            "X-RequestDigest": document.getElementById("__REQUESTDIGEST").value,
          },
        };
        axios
          .get(url, { data: JSON.stringify(data) }, configureAxios)
          .then(function (res) {
            console.log("response:", res);
        })
          .catch(function (e) {
            console.log("error:", e);
          });
        });
     

I get the response.data being a string of the XML data. I want to be JSON so I can access the values within. I'm possibly missing something in the headers? enter image description here

2 Answers 2

0

My test code for your reference:

 axios.get(`${_spPageContextInfo.webAbsoluteUrl}/_api/lists/getbytitle('test')/fields?$filter=TypeDisplayName eq 'Choice'`)
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });

Test Result: enter image description here

2
  • What did did you put for your headers? Commented Nov 5, 2020 at 15:58
  • I did not put the header parameter. Commented Nov 6, 2020 at 1:11
0

Okay, I figured out that I was sending data in my GET request! Huge mistake. I copied over for a different rest call I was making possibly a POST and it has the data piece.

 axios.get(url, configureAxios).then(function (res){}

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.