简体   繁体   中英

Creating REST parameters for jQuery consume webservice

I am using jQuery to consume a web service I built, the input is currently serialized JSON, as well as the output via jQuery AJAX.

I'd like to make the service more RESTful by adding URI query string parameters so that users can access the same same page of search results, query string, etc. etc., when they save the URI as their state.

I don't believe my web service needs much changing. Should I access and re-write the URI using jQuery? If so does anyone have any posts that demonstrate how to do this?

Thanks

Web service:

/// <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;
    }

And the Ajax request using JSON Serialization:

//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) {

I don't know about the web service, but on the jQuery side, if qtype , query , page and rP should be your parameters, just change:

data: JSON.stringify(req)

to

data: req.request

or just

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

Of course you have to change your web service to accept those GET parameters.

I hope I got your question right.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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