开发者

Creating REST parameters for jQuery consume webservice

开发者 https://www.devze.com 2023-01-10 06:55 出处:网络
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 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: "开发者_C百科/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.

0

精彩评论

暂无评论...
验证码 换一张
取 消