-2

I want to send the JSON data to other computer web API server. I can't send data with Axios in Reactjs but Postman is working. These computers work on the same network.


ERROR in console;

OPTIONS https://192.168.1.214:5001/api/books 405 (Method Not Allowed)

Access to XMLHttpRequest at 'https://192.168.1.214:5001/api/books' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Uncaught (in promise) Error: Network Error at createError (createError.js:17) at XMLHttpRequest.handleError (xhr.js:80)


Error image : https://i.resimyukle.xyz/830y7z.png

addAnswers = async (dispatch, e) => {
        e.preventDefault();

        const newItem = {
            "bookName" : "information",
            "price" : "11.11",
            "category" :"information",
            "author" : "yazar"
        }
        const response = await axios.post("https://192.168.1.214:5001/api/books",newItem);
        console.log(response);
devserkan
  • 16,870
  • 4
  • 31
  • 47
ahmetcamli
  • 37
  • 2
  • You can check the duplicate question. If you don't have any access to the API server then you have a few options here. You can look at the proxy option for create-react-app. – devserkan Jun 27 '19 at 12:58

1 Answers1

-2

You have to do like this

   let getRresponse;

   axios.post("https://192.168.1.214:5001/api/books",newItem)
    .then((response) => {
      getRresponse = response;       //get your response here like this
    });

    console.log(getRresponse);

Enjoy !

Vijay sadhu
  • 500
  • 4
  • 11
  • This code not solved my problem – ahmetcamli Jun 27 '19 at 13:08
  • @ahmetcamli this one is the standard structure of axios, if code was not working in your , then you do mistake somewhere else. – Vijay sadhu Jun 27 '19 at 13:14
  • @vijaysadhu, OP is using an async function and handling it properly with async/await. `axios` provides a promise, there isn't any standard structure handling for this. – devserkan Jun 27 '19 at 13:23
  • hey @devserkan why you still use async-await , there is no need to use them. – Vijay sadhu Jun 27 '19 at 13:38
  • What do you mean by "no need"? It is a new addition with ES2017. [Please read](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function#Description). – devserkan Jun 27 '19 at 13:47
  • hey @devserkan here you have misunderstanding ,i'm taking like, if you use my code, then you don't need to use async/await , and thanks man for doing R&D for me to give me this awesome doc. – Vijay sadhu Jun 27 '19 at 14:17
  • @vijaysadhu, We can use async/await easily here. OP does that and it is fine. async/await is newer then promises (we can use them mutually, not a replacement of course). Unfortunately, this isn't related with the real problem here. – devserkan Jun 27 '19 at 14:48