简体   繁体   中英

Extjs application with Json-server not working fine

I have a small app and I am using Rest Proxy. I set up json-server https://github.com/typicode/json-server locally.

I have not changed anything in server settings. I am able to successfully GET data from server but when I try to create data like this

var people = App.model.myModel;

var ed = new people({"id": 2,"title": "test","body": "test"});
ed.save();

Error appears in browser console is

PUT http://localhost:3000/posts/11?_dc=1427464731634 404 (Not Found)

Can some one point out why it is trying to PUT data and not POST data ?

I found the problem my self. I was sending the "id" as well and it was looking for a post with Id 2, and obviously that doesn't exist.

var people = App.model.myModel;
var ed = new people({"title": "test","body": "test"});
ed.save();

Works perfectly

PUT is used to update an item, Not Create.

As you have specified an id value ExtJs will presume that you need to update the record rather than create it, therefore making the PUT request.

Most RESTful API's will provide GET, PUT, POST and sometimes DELETE, LINK Endpoints for each entity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM