Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery put with WebApi - Not even calling the Controller method

Sorry to ask a question about this so soon after asking another question but I'm now struggling with the PUT.

I have a jQuery method which collects the data and passes it to a PUT function in the valuescontroller. But the controller isn't even being called (As I have a break point on it and it isn't breaking)

Can I just check my jQuery is correct?

var data = {
            id: truckId,
            obj: {
                TruckId: truckId,
                Reg: reg,
                Description: desc,
                Condition: condition
            }
        };
        var json = JSON.stringify(data)

        $.ajax({
            url: '/api/Values',
            type: 'PUT',
            contentType: "application/json; charset=utf-8",
            data: json,
            success: function (results) {
                $.getJSON("api/Values", LoadTrucks);
                alert('Truck Updated !');
            }
        })

The controller looks like this:

public void Put(int id, TruckInfo obj)
    {
        WebApiTestEntities db = new WebApiTestEntities();

        var data = from item in db.TruckInfoes
                   where item.TruckId == id
                   select item;
        TruckInfo oldRecord = data.SingleOrDefault();
        oldRecord.Reg = obj.Reg;
        oldRecord.Description = obj.Description;
        oldRecord.Condition = obj.Condition;
        db.SaveChanges();
    }

Now it looks to me like that should at least be reaching the Controller. My guess is the parameters aren't being passed correctly and therefore it is looking for a different method but I can't see why or how to rectify that.

Any help would be greatly appreciated :)

Lex

EDIT: As requested, further information.

No Javascript errors on the error console.

The Console log displays the following:

{"Message":"No HTTP resource was found that matches the request URI 'localhost:62997/api/Values'.","MessageDetail":"No action was found on the controller 'Values' that matches the request."}

And under MessageDetail for the JSON console I get this (Which supports my theory on incorrect parameters I think)

"No action was found on the controller 'Values' that matches the request"

The get however succeeds. (And the POST I got working earlier this morning.)

like image 263
Lex Eichner Avatar asked Feb 26 '13 11:02

Lex Eichner


People also ask

Can JavaScript call Web API?

The most Popular way to call a REST API from JavaScript is with the XMLHttpRequest (XHR) object. You can perform data communication from a URL of the Web API without having to do a full page refresh. Other methods for calling APIS in JavaScript are Fetch API and Promise.


2 Answers

Change your code like below, then it will work fine:

var data = {
        TruckId: truckId,
        Reg: reg,
        Description: desc,
        Condition: condition
    };

var json = JSON.stringify(data)

$.ajax({
    url: '/api/Values/' + truckId,
    type: 'PUT',
    contentType: "application/json; charset=utf-8",
    data: json,
    success: function (results) {

    }
})

The best practice when using REST is:

  1. User POST when creating new resource.
  2. User PUT when updating existing resource and the Id of resource should be in the URL.
  3. User DELETE when deleting existing resource and the Id of resource should be in query string as well.
like image 94
cuongle Avatar answered Oct 18 '22 17:10

cuongle


You are doing a PUT request without specifying the ID of the object.

See here.

the URI in a PUT request identifies the entity enclosed with the request

Change

url: '/api/Values',
type: 'PUT',

To

url: '/api/Values/' + truckId,
type: 'PUT',

The Routing engine will need to id to match to your method

public void Put(int id, TruckInfo obj)

At the moment you are attempting to put this in your model here:

id: truckId,
obj: {
   TruckId: truckId,
   //etc

But this will not work. Only one object is deserialisable from the body of the request.

Simply alter your message payload to this

var data = {
            TruckId: truckId,
            Reg: reg,
            Description: desc,
            Condition: condition
           }

And use the correct url structure e.g.:

PUT http://localhost:62997/api/Values/1
like image 44
Mark Jones Avatar answered Oct 18 '22 19:10

Mark Jones