【问题标题】:Getting Badrequest error while parsing json object from client side to WCF service将 json 对象从客户端解析到 WCF 服务时出现 Badrequest 错误
【发布时间】:2013-06-10 13:04:19
【问题描述】:

客户:

<script src="http://code.jquery.com/jquery-nightly.min.js" type="text/javascript">      </script>
<script src="JSON.js" type="text/javascript"></script>
<script type="text/javascript">
    function register() {
        var searchRequest = new Object();
        searchRequest.Name = "Chris";

        //var dataToSend = '{"searchRequest":[' + JSON.stringify(searchRequest) + ']}';
        var dataToSend = "JSON";

        var resolution = { r: { Name: "Fred", Rank: 2, SerialNumber: 17268 } };

        // convert object to JSON string  (See http://jollytoad.googlepages.com/json.js)
        var objectAsJson = $.toJSON(resolution);
        alert(objectAsJson);
        $.ajax({
            cache: false,
            type: "POST",
            async: false,
            url: "http://localhost:64202/MyService.svc/" + objectAsJson ,
            data: objectAsJson ,
            //data: dataToSend,
            contentType: "application/json",
            dataType: "html",
            processData: false,
            success: function (data)
            {
                $('body').html(data);
            },
            error: ServiceFailed
        });
    }
    function ServiceFailed(result) {
        alert(result.statusText);
        alert("Failed");
    }
    function ServiceSucceeded(result) {
        debugger;
        alert("Success; " + result.GetDateTimeResult);

    }

    $(document).ready(function () {
        $("#btnGet").click(function () {
            register();
        });
    });

服务接口

[OperationContract, WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped,    RequestFormat = WebMessageFormat.Json,UriTemplate="{reg}")]
//Stream GetDateTime(string message, int x, int y);
string GetDateTime(string reg);

服务

public string GetDateTime(string reg)
{
   //reg = "{\"EMail\": \"hiren@gmail.com\",\"Name\": \"Hiren\",\"Age\": 23,\"Zipcode\": 85 }";
   DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(RegistrationForm));
   UTF8Encoding uniEncoding = new UTF8Encoding();
   MemoryStream stream1 = new MemoryStream();
   stream1.Position = 0;
   var sw = new StreamWriter(stream1, uniEncoding);
   //try
   //{
   sw.Write(reg);
   sw.Flush();//otherwise you are risking empty stream
   stream1.Seek(0, SeekOrigin.Begin);

   // Test and work with the stream here. 
   // If you need to start back at the beginning, be sure to Seek again.
   //}
   //finally
   //{
   //    sw.Dispose();
   //}

   RegistrationForm obj = (RegistrationForm)ser.ReadObject(stream1);
   //var address = new JavaScriptSerializer().Deserialize<RegistrationForm>(reg);
   // return "sa:" + reg.EMail;
   return "sa:";
}

【问题讨论】:

  • 为什么要将 objectAsJson 连接到 url?也尝试设置 dataType: "json" 并删除 contenttype。

标签: c# json wcf-data-services


【解决方案1】:

尝试从 url 中删除 objectAsJson。放入你的界面

[OperationContract, WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped,  
  RequestFormat = WebMessageFormat.Json,UriTemplate="/GetDateTime")]       
    string GetDateTime();   

像你一样传递你的数据。

【讨论】:

    【解决方案2】:

    您向我指出的想法解决了问题,只需我们不需要做其他事情即可使其正常工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-16
      • 1970-01-01
      • 2012-01-07
      相关资源
      最近更新 更多