【问题标题】:Retrieve json object as querystring using wcf Restful Service使用 wcf Restful Service 检索 json 对象作为查询字符串
【发布时间】:2013-07-09 13:07:08
【问题描述】:

我正在创建一个 WCF Restful 服务,我想通过 POST 操作将来自 javascript 的 json 对象保存为查询字符串。我将 json 对象作为查询字符串获取,但无法在 wcf restful 中访问它,请帮助。 .........

我的javascript代码

var myRequest = new XMLHttpRequest();


myRequest.onreadystatechange=function(dataString) {

    if (myRequest.readyState == 4 && myRequest.status == 200) {


        console.log('Sent to server: ' + dataString + '');
        window.localStorage.removeItem(dataString);
    }
    else if (myRequest.readyState == 4 && myRequest.status != 200) {

        console.log('Server request could not be completed');
        saveDataLocally(dataString);
    }
}
    var url="http://localhost:58168/RestServiceImpl.svc/json";
myRequest.open("POST",url+"?"+dataString,true);
    myRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    myRequest.send(dataString);

   alert('Saved to server: ' + dataString + '');

我在 javascript 中的请求

![将 json 对象作为查询字符串发送到 wcf RESTful 服务器时][1]

>**RequestURL**:http://localhost:58168/RestServiceImpl.svc/json?"firstName":"shuresh","lastName":"kumar"}

请求标头: 内容类型:application/x-www-form-urlencoded 来源:chrome-extension://ddijiilbgbjgciahjmonfahapadmkcfp 用户代理:Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko)Chrome/27.0.1453.116 Safari/537.36

查询字符串参数 {"firstName":"shuresh","lastName":"kumar"}:

表单数据 {"firstName":"shuresh","lastName":"kumar"}:

WCF Restful 服务契约

 [OperationContract]
    [WebInvoke(Method = "POST",
        ResponseFormat = WebMessageFormat.Json,
         RequestFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.Wrapped,
        UriTemplate = "json?dataString={dataString}")]
    string JSONData(string dataString);

RestServiceImpl.svc.cs

  public string JSONData(string dataString)
    {

        return "You requested product " + dataString;


    }

我无法访问服务合同中的 json 对象(dataString)。请帮助我如何访问附加在上述 wcf restful 代码中的查询字符串中的 json 对象。

http://localhost:58168/RestServiceImpl.svc/json?"firstName":"shuresh","lastName":"kumar"}

【问题讨论】:

    标签: c# javascript json wcf


    【解决方案1】:

    您不会将所有内容都放在“一个字符串”下。

    试试这个(只是为了展示概念)

    public string JSONData(string firstNameArg)
        {
    //your code here
    }
    

    然后

    http://localhost:58168/RestServiceImpl.svc/json?"firstNameArg":"shuresh"}
    

    看看会发生什么。

    【讨论】:

      【解决方案2】:

      在 POST/PUT 时,表单数据不在 QueryString 中,相反,您可以在请求的 InputStream 中找到它。 此外,您的 QueryString 格式不正确...?param=value&param=value... 希望对你有帮助

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-07-14
        • 1970-01-01
        • 2018-05-15
        • 2014-12-16
        • 1970-01-01
        • 1970-01-01
        • 2020-04-16
        相关资源
        最近更新 更多