0

i want to use this ajax command in fetch command in react js

  var data = {
                resource_id: '5f5afc43-639a-4216-8286-d146a8e048fe', // the resource id
            };
            $.ajax({
                url: 'https://data.gov.il/api/action/datastore_search',
                data: data,
                dataType: 'json',
                success: function (data) {
                    console.log(data);
                }
            });

how can i use that command in Fetch command?

3 Answers 3

2

Here is an example on how to use fetch, this example use JSON placeholder

fetch("https://jsonplaceholder.typicode.com/todos/1", {
  headers: { ContentType: "application/json" },
})
  .then((response) => response.json())
  .then(console.log);

for your code it might be something like this but, unfortunatly the API you are requesting requires access token, so its not tested:

var data = {
  resource_id: '5f5afc43-639a-4216-8286-d146a8e048fe', // the resource id
};
fetch("https://data.gov.il/api/action/datastore_search", {
  headers: { ContentType: "application/json" },
  body: JSON.stringify(data);
})
  .then((response) => response.json())
  .then(console.log);

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

Comments

0
fetch('https://data.gov.il/api/action/datastore_search', {
headers: {'ContentType': 'application/json'},
body: JSON.stringify(data)
}).then(response => response.json()).then(console.log)

5 Comments

Unhandled Rejection (TypeError): Failed to execute 'fetch' on 'Window': Request with GET/HEAD method cannot have body.
is your environment the browser or nodejs?
my environment is browser
you might have an old browser that does not support fetch? try using: github.com/github/fetch
While this code might solve the OP's question, it is better to add context, or an explanation to help future visitors understand this solution to prevent similar issues in their own code, and help users focus on what is important or unique about this code.
0

i succeeded, i just put the ajax command in the react project and it's worked

Comments

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.