RGdent RGdent - 7 months ago 37
Javascript Question

Javascript Objects?

I have two Question which I am uncertain of.



  1. What happens and what are the rules when using this piece of coding:




var c = "Test";

$scope.data = {c : c};


The output returns
[object Object]
and console shows this value
Object {c: "Test"}




  1. If this value is returned to a C# based service how can I extract the value c from the object correctly?




EDIT:
I am using WCF RESTful service and my url looks like:

$http.post("http://localhost/TestService.svc/testdata/" + $scope.data)

Answer

When you do a POST to a service you should send the data as post data, as described in the docs for $http.

var url = "http://localhost/TestService.svc/testdata/";
var postData = { c: "Test" };
$http.post(url, postData);

Remember to configure your server action with Method="POST" to be able to receive POST data. The deserialization from the posted data is handled by WCF.


Another option is of course to use a PUT method, and append your data as query parameters to the URL. The URL would in that case look something like

http://localhost/TestService.svc/testdata/?c=Test

If you have nested data in your model this will soon get tricky to handle though, and in that case a POST is the better option. Also; If you use PUT, remember to not expose your service to CSRF vulneratibilities in which an attacker gives a link to an authenticated user which then performs some unwanted actions.

Comments