wysyłanie danych metodą fetch zwraca mi null

0

mam wysłać dane pod adres gdzie struktura wygląda tak: (Name,X,Y,RouteId,Description pobieram z formularza). Niestety zwraca mi null i nic nie wysyła..

"PointList": {
[{
          "Name": Name,
          "Y": Y,
          "X": X,
          "RouteId": RouteId,
          "Description": Description,
        }]
}

najpierw dodaje sobie taka strukture do tablicy

  appendElement = (Name, Description, X, Y, RouteId) => {
    this.setState({
      elements: ([...this.state.elements, {
        "PointList": ([{
          "Name": Name,
          "Y": Y,
          "X": X,
          "RouteId": RouteId,
          "Description": Description,
        }])
      }]),
    })

i np mam jak mam 3 obiekty w elements to po kliknieciu utworz:

  handleOnHistoryPush = (e) => {
    fetch("http://dearjean.ddns.net:44301/api/Add", {
      method: "post",
      headers: {
        "Content-Type": "application/json"
      },
      body: JSON.stringify({ ...this.state.elements })
    })
      .then(res => res.json())
      .then(res => {
        console.log(res);
      })
  this.setState({ elements: ([]) });
  };
2

Ale co i w którym miejscu Ci zwraca null ?
Serwer zwraca null ? Fetch zwraca null ?

I pokaż co masz w konsoli. Może coś jest blokowane?

U mnie jest:
screenshot-20200507142403.png

1

Wysłałem Postmanem

{
	"PointList":  
		[{
          "Name": "Name",
          "Y": "Y",
          "X": "X",
          "RouteId": "RouteId",
          "Description": "Description"
        }]
 
}

i otrzymałem

{
   "Points3s": [
       {
           "Id": 94,
           "Name": "Name",
           "Description": "Description",
           "X": 0.0,
           "Y": 0.0,
           "RouteId": 98,
           "Route": {
               "Id": 98,
               "Name": "Trasa",
               "Description": "Brak"
           }
       }
   ],
   "route": {
       "Id": 98,
       "Name": "Trasa",
       "Description": "Brak"
   }
}

więc API działa, ale mogłoby mogłoby zwracać jakąś informację o błędzie zamiast nulla, chociażby status HTTP.
Natomiast problem z CORS może wynika z braku ustawień proxy, jeśli używasz Create React App https://create-react-app.dev/docs/proxying-api-requests-in-development/

0

zwraca mi ten adres dearjean.ddns.net:44301/api/Points3/
i jak mam wykorzystac teraz metode put?
dearjean.ddns.net:44301/api/Points3/id
gdzie id jest id punkty (points3)
i np dearjean.ddns.net:44301/api/Points3/88
jak putem zmienic RouteId?potrzebne mi jest to gdyż
wtedy dane trasy beda sie tutaj zapisywać - nizej widac ze RouteId =1
http://dearjean.ddns.net:44301/api/Points3/RouteOnNumber?id=1&fbclid=IwAR0lGCixXK1oYQEW6GIKfY7h6A7QTWqk0CrQzrCg8lT74IpPQNQVEhgNfxs

1

Wyślij JSONa metodą PUT pod URL http://dearjean.ddns.net:44301/api/Points3/id

{
	"Name": "Name",
	"Y": ..,
	"X": ...,
	"RouteId": ..,
	"Description": ...
}

A tak na serio, lepiej dowiedzieć się najpierw co przyjmuje i zwraca API zamiast wysyłać requesty i patrzeć co się stanie :)

0

a wiesz moze czemu mi nie działa takie wysyłanie?

 appendElement = (Name, Description, X, Y, RouteId) => {
    this.setState({
      elements: ([...this.state.elements, {
        "PointList": ([{
          "Name": Name,
          "Y": Y,
          "X": X,
          "RouteId": RouteId,
          "Description": Description,
        }])
      }]),
    })
handleOnHistoryPush = (e) => {
    fetch("http://dearjean.ddns.net:44301/api/Points3/Add ", {
      method: "post",
      headers: {
        "Content-Type": "application/json"
      },
      body: JSON.stringify({ ...this.state.elements })
    })
      .then(res => res.json())
      .then(res => {
        console.log(res);
      })
  this.setState({ elements: ([]) });
  };
}

Musze w ten sposob to wysyłac :

handleOnHistoryPush = (e) => {
 const newItem = {
      "PointList": ([{
        "Name": Name,
        "Y": Y,
        "X": X,
        "Id": RouteId,
        "Description": Description,
      }]),
    }
    fetch("http://dearjean.ddns.net:44301/api/Points3/Add", {
      method: "post",
      headers: {
        "Content-Type": "application/json"
      },
      body: JSON.stringify(newItem)
    })
      .then(res => res.json())
      .then(res => {
        console.log(res);
      })
}
0

Zapytaj twórcy API czemu przyjmuje jeden obiekt zamiast kolekcji.
To, że wysyłanie nie działa w tym albo innym przypadku jest spowodowane tym, że nie znasz struktury JSONów, które masz wysyłać.

1 użytkowników online, w tym zalogowanych: 0, gości: 1