简体   繁体   中英

$http put v create - what is the difference between the data object returned from .success() in each (JavaScript)

Is there a fundamental difference between " data " variable returned from .success() in $http.create v $http.put ?

I've added " console.log(data) " to 2 different functions below (and included the Rails controllers for reference) so we can see the difference in output.

Create

i.create = function(item) {
    return $http.post('/items.json', item).success(function(data) {
        console.log(data);
    });
};

def create
    respond_with Item.create(item_params)
end

Put

i.edit = function(id, item) {
    return $http.put('/items/' + id + '.json', {item: item}).success(function(data) {
        console.log(data);
    });
};

def update
    item = Item.find(params[:id])
    item.update_attributes(item_params)
    respond_with item
end

The first function prints below to console, ( which includes the object )

app.self-58d6f492fc5c0efae12da42ede8f7ad0257b7e5ce84e944443b2beecbb803e20.js?body=1:165
<br>
 Object {id: 22, desc: "testabc", list_id: null, created_at:
"2016-03-10T02:47:05.194Z", updated_at: "2016-03-10T02:47:05.194Z"…}

The second function prints below to console, ( no object )

app.self-58d6f492fc5c0efae12da42ede8f7ad0257b7e5ce84e944443b2beecbb803e20.js?body=1:186

Both functions work and do their job. But "data" returned from the PUT does not contain the object.

Why does it happen this way?

there's no fundamental difference between the two, the only thing that makes them difference is the data response from the server. The update controller of your server might not responding json object.

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