0

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)
7
  • it is getting coerced to a string, objects when coerced to a string look like [object Obect]; Commented Apr 29, 2016 at 7:11
  • 1
    What do you mean 'the rules when using this piece of coding'? Commented Apr 29, 2016 at 7:11
  • "To a C# based service"? What service is that, it very much depends on the technology you choose. Basically you get a POST request to your server and handle it in some sort of controller action. Commented Apr 29, 2016 at 7:13
  • When you are posting object data you shouldn't append it as put parameters. Send it as POST data instead. $http.post(url, postData). Commented Apr 29, 2016 at 7:44
  • @Patrick For some reason my browser does not allow it I still get a 404 error? Commented Apr 29, 2016 at 7:52

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

When you try to convert an object to a string (which happens when you try to display it), you get the text [object Object], indicating that it is an object without any further disclosure of its properties.

When you want to pass the data to other applications, a common format is JSON, which looks like the string your get in the console. C# as well as most other modern languages have built-in functionality or libraries to parse JSON into their own data structures.

To explicitly convert an object to JSON, use JSON.stringify.

var c = "Test";
var data = {c : c};
alert(JSON.stringify(data));

2 Comments

When I use JSON.stringify my url fails with a 404 error, but when parsing data as `[object Object] it get's send through?
@RGdent: But you haven't mentioned what server technology you are using. How are you sending the data? JSON.stringify was not meant for you to use to "send to the server", it was a way of seeing the object as a string. You need to give us more information if you want us to be able to help you
0

You're a little short on details in your question. Is this AJAX? Form postbacks? URL parameters? Headers? Websockets? How are you sending this data to the server? My answer assumes you're doing AJAX with the content in the body of the request.

It's important to understand that "returning to a C# service" isn't a problem. Whenever you make an HTTP request, you're serializing the data in a certain way. Probably in this case you're serializing it to JSON. The server reads the request, parses the JSON, and makes its own interpretation of it. So as long as you generate valid JSON (which your libraries will certainly do behind the scenes), the server won't know exactly how you generated it.

Secondly, JSON serializers don't all follow the same convention. JS is perfectly happy to have a key not be quoted, which is what you're observing.

foo = {
    abc: 123, <--- "abc" is a literal string here, even if it isn't quoted
    "abc": 456  <--- same key, different value
}

Most other language libraries require key strings to be quoted, JS doesn't.

So to answer your question "how can i extract the value c", the answer is, just like any other key! Assuming you deserialize the request body to a variable called data, then you'd just do data["c"].

4 Comments

I am using url param's when posting data.. could you maybe give me an example on how you deserialize the values of data?
@RGdent url parameters are very limited, and it's not easy to represent entire objects in them. Think of url parameters as a Dictionary<String, String>. Do you want the whole object to be deserialized on the other end (which has, perhaps, more than one field), or are you able to make it so that your values would fit into a "string-to-string" mapping?
I want it to be deserialized entirely from the other side but it only has one field inside it?
If you can guarantee that you'll only ever want one field, then it's best to just add it as a normal parameter, so that your URL will look something like https://example.com?c=foo, and (depending on your server framework/setup) you'll be able to read it with msdn.microsoft.com/en-us/library/… , or, succinctly, Request.Params["c"]. No need to go into json serialization/deserialization for one field.
0
console.log($scope.data + ""); // [object Object]
console.log($scope.data); // the actual object
  1. If an object is coerced to a string, like in the above example.
  2. If .toString(); is called on an object.

[object Object] is displayed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.