Question

I'm having trouble performing a PUT Request over HTTP to a WCF Data Service (OData). The problem is the ID. It isn't autogenerated and I can't change the DB Settings (not allowed in this case). So when I try to submit an ID, it sends the right ID but not the right type...

Apparantly the service expects an Int64 for the ID and isn't able to parse my input. Here is the code:

 function OnCreateDisplay() {
     $('#DisplayInfoLoader').html('<span style="color: orange;">Creating object....</span>');
     $('#DisplayInfoLoader').fadeIn(1000);

     var url = "....";

     var r = new Object();
     r.DisplayID = NextDisplayID+"L";
     r.Name = $("#FDisplayName").val();
     r.Code = parseInt($("#FDisplayCode").val());
     r.Status = $("#FDisplayStatus").val();
     r.ProjectID = selected_project+"L";
     r.Description = $("#FDisplayDescription").val();
     jr = JSON.stringify(r);
     alert(jr);

     $.ajax({
         type: "PUT",
         url: url,
         data: jr,
         contentType: "application/json; charset=utf-8",
         success: function (result) {
             $('#DisplayInfoLoader').html('<span style="color: green;">Display created....</span>');
             $('#DisplayInfoLoader').fadeOut(3000);
         },
         error: function (xhr, ajaxOptions, thrownError) {
             alert(xhr.responseText);
             $('#DisplayInfoLoader').html('<span style="color: red;">An Error occured....</span>');
             $('#DisplayInfoLoader').fadeOut(3000);
         }
     });

     LoadProjectDisplays();

     return false;
 }

the function NextDisplayID() just gets the last inserted ID and increments it. It works like a charm. I've tried adding a +"L" after it (WCF loves that for longs...) but it just won't parse!

EDIT:

I'm sending this JSON String:

{"DisplayID":"132L","Name":"Name","Code":"Code","Status":"0","ProjectID":"1L","Description":"Descr"}

On this url: "http://" + ip + ":8989/Service.svc/Displays("+NextDisplayID+"L)"

IP is a lan-ip 192.168.0.191

Also when I keep playing with the code... I sometimes get "Resource not found for the segment 'Displays'." as error

PUT HEADER:

PUT /Service.svc/Displays(132L) HTTP/1.1

Host: 192.168.0.191:8989

Connection: keep-alive

Content-Length: 110

Origin: http://192.168.0.191:8989

X-Requested-With: XMLHttpRequest

User-Agent: Mozilla/5.0 (Windows NT 6.0) AppleWebKit/535.11 (KHTML, like Gecko)

Chrome/17.0.963.56 Safari/535.11

Content-Type: application/json; charset=UTF-8

Accept: /

Referer: http://192.168.0.191:8989/

Accept-Encoding: gzip,deflate,sdch

Accept-Language: en-GB,nl;q=0.8

Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3

TEXT:DATA example {"DisplayID":"132L","Name":"fdsafsda","Code":"dsafsda","Status":"0","ProjectID":"1L","Description":"fdsafsad"}

tnx for reading this

Était-ce utile?

La solution

Int64 is serialized in JSON as the number written as string. So for example "12345" (including the quotes). This is described in here: http://www.odata.org/developers/protocols/json-format#PrimitiveTypes

Note that it's different than the format used in the URL, where the format is the number followed by L (which you already use).

As for POST versus PUT, they have different meaning.

POST is used for creating a new entity, and is sent to the entity set URL, so .../Displays

PUT is used to update an exising entity, and is sent to the entity instance URL (the entity you want to update), so .../Displays(1234L). Also note that updating key property (in your case the DisplayID) is usually not allowed, and the server will likely ignore the value you send from the client in the PUT. So you can leave it out from the payload in case of PUT.

Autres conseils

Try using POST instead of PUT... for it is also for creating items. You might have to adjust the URL and maybe try sending with or without DisplayID ?

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top