简体   繁体   English

将查询参数从HTML / JS应用传递到Azure服务器脚本

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

I am currently working on an HTML/JS App using Windows Azure Mobile Service. 我目前正在使用Windows Azure移动服务开发HTML / JS应用程序。 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))]. 我有两个表,一个表存储有关吸引力的信息[attractionTable(attractionId,address,...)],一个表跟踪哪个用户喜欢哪个吸引力[favoriteAttractionTable(atavourId,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: 在服务器端,我将favoriteAttractionTable的读取脚本编辑为此:

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 在我的JavaScript代码中,我正在使用以下请求

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. 我基本上获得了任何用户添加到他的收藏夹中的所有景点的信息,但是我想修改var sql,使其仅接收与适当的userId相关的内容。

query.getComponents();

shows queryString: '((fav eq true) and (userId eq 0))' in my server script. 在我的服务器脚本中显示queryString:'{{fav eq true)and(userId eq 0))'。 But I am wondering how I can access this information?! 但是我想知道如何访问此信息?!

request.parameters.fav

is supposedly undefined. 据说是不确定的。

Any advice is highly appreciated :) 任何建议都受到高度赞赏:)

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. 这些参数将通过服务器端的request.parameters对象进行访问。

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. 如果您有兴趣,这里有更多详细信息:当您使用where方法时,您正在传递一些模板对象,该对象将转换为OData $filter表达式 ,并传递给服务器。 The server will then parse that and convert it into the appropriate SQL WHERE clause to be sent to the database. 然后,服务器将对其进行解析,并将其转换为适当的SQL WHERE子句,以将其发送到数据库。 So your original code will send a request similar to this one (spaces will be replaced with %20 , but left them here for clarity): 因此,您的原始代码将发送与该请求类似的请求(空格将替换为%20 ,但为了清楚起见,将其留在此处):

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. 如您getComponents ,您可以通过getComponents函数访问它,但是到那时,您必须自己解析表达式以检索比较的值。 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). 您可以直接从request.parameters检索这些值(这些值的类型将是字符串,因此,如果要将它们视为布尔值的数量,则需要进行适当的转换)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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