我有两个不确定的问题。
- What happens and what are the rules when using this piece of coding:
var c = "Test";
$scope.data = {c : c};
输出返回[object Object]
并且控制台显示该值Object {c: "Test"}
- If this value is returned to a C# based service how can I extract the value c from the object correctly?
编辑: 我正在使用 WCF RESTful 服务,我的 url 如下所示:
$http.post("http://localhost/TestService.svc/testdata/" + $scope.data)
最佳答案
当您对服务执行 POST 时,您应该将数据作为发布数据发送,如 docs for $http 中所述。 .
var url = "http://localhost/TestService.svc/testdata/";
var postData = { c: "Test" };
$http.post(url, postData);
请记住使用 Method="POST"
配置您的服务器操作,以便能够接收 POST 数据。发布数据的反序列化由 WCF 处理。
另一种选择当然是使用 PUT 方法,并将数据作为查询参数附加到 URL。在这种情况下,URL 看起来像
http://localhost/TestService.svc/testdata/?c=Test
如果您的模型中有嵌套数据,那么处理起来很快就会变得棘手,在这种情况下,POST 是更好的选择。还;如果您使用 PUT,请记住不要将您的服务暴露给 CSRF vulneratibilities其中攻击者向经过身份验证的用户提供链接,然后该用户执行一些不需要的操作。
关于JavaScript 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36931778/