简体   繁体   English

将JSON数据发送到WCF服务?

[英]Sending JSON data to WCF service?

I have a WCF REST Service which is having two simple methods. 我有一个WCF REST服务,其中有两种简单的方法。

[OperationContract]
    [WebInvoke(Method="GET",
               ResponseFormat=WebMessageFormat.Json,
               RequestFormat=WebMessageFormat.Json,
               UriTemplate = "request/{controlType}")] 
string GetJSONConfig(string controlType);

[OperationContract]
(Method = "POST", 
        ResponseFormat = WebMessageFormat.Json, 
        RequestFormat = WebMessageFormat.Json, 
        UriTemplate = "save/{jsonString}")]
 string SaveJSON(string jsonString);

the first method is getting called from the javascript code. 第一种方法是从javascript代码中调用。 but where I to send the JSON data to the second and getting 404 error. 但是我将JSON数据发送到第二个并且出现404错误。

have anyone faced this type problem. 有没有人遇到这种类型的问题。

  $(document).ready(function () {

        var circle = function () {
            this.x = 100;
            this.y = 100;
            this.r = 10;
        };
        var x = new circle();
        var arr = [];
        arr.push(x);
        var jsonData = JSON.stringify(arr);

        $('#serviceCall').click(function () {
            $.ajax(
            {
                url: 'http://localhost:52506/JsonDataService.svc/save/',
                type: "POST",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                data: JSON.stringify(arr),
                processData: true,
                success: function (data) {
                    document.getElementById("data").value = data;
                },
                error: function (data) {
                    document.getElementById("data").value = data;
                }
            });
        });
    });

this is the javascript codebase. 这是javascript代码库。

The way you are trying to reach POST call is incorrect. 您尝试接通POST呼叫的方式不正确。

I'd like to suggest you to create DTO class, which is in fact projection of your JSON. 我想建议您创建DTO类,它实际上是JSON的投影。

WCF will map it automatically, using DataContractSerializer, if I'm not mistaken. 如果我没有记错的话,WCF将使用DataContractSerializer自动映射它。

sample: 样品:

 [OperationContract]
 [WebInvoke(UriTemplate = "/PlaceOrder", 
  RequestFormat= WebMessageFormat.Json,   
  ResponseFormat = WebMessageFormat.Json, Method = "POST")] 
  bool PlaceOrder(OrderContract order);

In case you'd like raw stream, do something like this: 如果您想要原始流,请执行以下操作:

[OperationContract(Name = “MyMethod”)]
        [WebInvoke(Method = “POST”,
         UriTemplate = “blablahblah”)]
        string Method(Stream data);

Its a bit of Syntax mistake! 它有点语法错误! Here is the correct one. 这是正确的。

[OperationContract]
[WebGet(ResponseFormat=WebMessageFormat.Json,
        UriTemplate = "request/{controlType}")] 
string GetJSONConfig(string controlType);

When you are using GET , you don't need to specify Request Format most of the time. 使用GET时 ,通常无需指定请求格式。 Because you can call GET method from URL. 因为您可以从URL调用GET方法。

For POST 对于POST

[OperationContract]
WebInvoke(Method = "POST", 
    ResponseFormat = WebMessageFormat.Json, 
    RequestFormat = WebMessageFormat.Json, 
    UriTemplate = "save/{jsonString}")]
string SaveJSON(string jsonString);

Here, you don't need to specify UriTemplate = "save/{jsonString}" , instead UriTemplate = "save" will do the job. 在这里,您不需要指定UriTemplate = "save/{jsonString}" ,而是UriTemplate = "save"可以完成工作。 .NET will automatically convert jsonString into JSON for you. .NET将自动为您将jsonString转换为JSON。 You just need to send JSON from client (in your JS code). 您只需要从客户端发送JSON(在您的JS代码中)。 I hope it helps you! 希望对您有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM