【问题标题】:Creating REST parameters for jQuery consume webservice为 jQuery 使用 webservice 创建 REST 参数
【发布时间】:2010-08-04 00:08:18
【问题描述】:

我正在使用 jQuery 来使用我构建的 Web 服务,输入当前是序列化的 JSON,以及通过 jQuery AJAX 的输出。

我想通过添加 URI 查询字符串参数使服务更加 RESTful,以便用户在将 URI 保存为他们的状态时可以访问相同的搜索结果页面、查询字符串等。

我认为我的网络服务不需要太多改变。我应该使用 jQuery 访问和重写 URI 吗?如果是这样,是否有人有任何帖子演示如何做到这一点?

谢谢

网络服务:

/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService
{
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public OutputData updateProductsList(InputData request)
    {
        //...//

        return result;
    }

以及使用 JSON 序列化的 Ajax 请求:

//Build the ajax Request
    var req = { request: { qtype: "ProductName", query: queryText, page: resultPage, rp: rP} };

    $.ajax({
        type: "POST",
        url: "/webservice/WebService.asmx/updateProductsList",
        data: JSON.stringify(req),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {

【问题讨论】:

    标签: jquery ajax json rest


    【解决方案1】:

    我不了解网络服务,但在 jQuery 方面,如果 qtypequerypagerP 应该是您的参数,只需更改:

    data: JSON.stringify(req)
    

    data: req.request
    

    或者只是

    var request: { qtype: "ProductName", query: queryText, page: resultPage, rp: rP} };
    //and
    data: request
    

    当然,您必须更改您的网络服务以接受这些 GET 参数。

    我希望你的问题是正确的。

    【讨论】:

    • 嗯,我想我明白了,我需要更改从页面获取输入数据的方式以接受 URI 数据?我还在使用 JSON 发送数据吗?还是表格帖?
    • @jordan.baucke:正如我所说,我不能对网络服务说太多,我什至不知道这是什么语言;)如果用户应该能够为页面添加书签,你必须从 POST 更改为 GET (type: GET) 并使 Web 服务接受这些参数。如果像我描述的那样将数据传递给 Ajax 调用,jQuery 将调用 URL /webservice/WebService.asmx/updateProductsLis?qtype=...&amp;query=...&amp;...
    • 感谢 Felix 我最终使用:jQuery BBQ benalman.com/code/projects/jquery-bbq 来获取参数并重新编写 URI。如果我想更直接地访问我的网络服务,我会更新它。
    猜你喜欢
    • 1970-01-01
    • 2012-09-23
    • 1970-01-01
    • 2014-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-06
    相关资源
    最近更新 更多