Passing Query Parameters from HTML/JS App to Azure Server Script

一个人想着一个人 提交于 2019-12-11 04:04:55

问题


I am currently working on an HTML/JS App using Windows Azure Mobile Service. I have two tables, one storing information about an attraction [attractionTable(attractionId, address,...)] and one keeping track of which user likes which attraction [favoriteAttractionTable(attractionId, userId, fav(bool))].

If the client wants to have a list of his favorite attractions there should only be one request to the server. On the server side I edited the read script of the favoriteAttractionTable to this:

function read(query, user, request) {

   var sql =
       "FROM routesTable rt, favoriteAttractionTable ft " +
        "WHERE rt.id = ft.routeId;";

    mssql.query(sql, {
        success: function(results) {
            request.respond(statusCodes.OK, results);
        }});

}

In my JavaScript Code I am using the following request

function loadFavoriteAttractions(){
    favTable = client.getTable("favoriteAttractionTable");
    var query = favTable.where({
            fav: true,
        userId : 0
        }).read().done(function (results) {
            console.log(results);
        }, function (err) {
            alert("Error: " + err);
        });
}

I basically get the information of all attractions that any user has added to his favorites but I want to modify the var sql in a way to receive only the ones related to the appropriate userId.

query.getComponents();

shows queryString: '((fav eq true) and (userId eq 0))' in my server script. But I am wondering how I can access this information?!

request.parameters.fav

is supposedly undefined.

Any advice is highly appreciated :)


回答1:


If you want to access request parameters in the scripts, your best bet is to use the custom query string parameters, something like the code below:

function loadFavoriteAttractions(){
    favTable = client.getTable("favoriteAttractionTable");
    var query = favTable.read({
        fav: true,
        userId : 0
    }).done(function (results) {
        console.log(results);
    }, function (err) {
        alert("Error: " + err);
    });
}

Those parameters will be accessed via the request.parameters object on the server side.

If you're interested, here's more details: when you use the where method, you're passing some template object which will be converted into an OData $filter expression that is passed to the server. The server will then parse that and convert it into the appropriate SQL WHERE clause to be sent to the database. So your original code will send a request similar to this one (spaces will be replaced with %20, but left them here for clarity):

GET .../tables/favoriteAttractionTable?$filter=((fav eq true) and (userId eq 0))

As you found out, you can access it via the getComponents function, but at that point you'll have to parse the expression yourself to retrieve the values of the comparisons. If you want to access some values passed by the client, as I mentioned the easiest way is to pass them as "normal" query-string parameters. In the code I sent, the request that will be sent will be similar to

GET .../tables/favoriteAttractionTable?fav=true&userId=0

And those values you can retrieve from the request.parameters directly (the type of the values will be strings, so if you want to treat them as numbers of Boolean values you'll need to do the appropriate conversion).



来源:https://stackoverflow.com/questions/17415257/passing-query-parameters-from-html-js-app-to-azure-server-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!