从HTML / JS应用程序传递查询参数的Azure服务器脚本 [英] Passing Query Parameters from HTML/JS App to Azure Server Script

查看:133
本文介绍了从HTML / JS应用程序传递查询参数的Azure服务器脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前工作的一个HTML / JS应用程序中使用的Windows Azure移动服务。
我有两个表,大约一个景点一个贮存信息[attractionTable(attractionId,地址,...)]和其用户喜欢它的吸引力1保持跟踪[favoriteAttractionTable(attractionId,用户id,最爱(布尔))。

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))].

如果客户希望有他最喜欢的景点列表应该只有一个请求到服务器。在服务器端,我编辑了favoriteAttractionTable的读取脚本这样的:

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

}

在我的JavaScript code我使用下面的请求

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

我基本上得到的任何用户已添加到他的最爱所有景点的信息,但我想修改VAR SQL的方式来接收不仅关系到适当的用户id的人。

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();

显示的queryString:'((最爱EQ真)和(用户ID EQ 0))'在我的服务器脚本。
但我想知道我怎么能访问这些信息?!

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.

任何意见是高度AP preciated:)

Any advice is highly appreciated :)

推荐答案

如果您要访问的脚本请求参数,最好的办法是使用自定义的查询字符串参数,像下面的code:

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

这些参数将通过在服务器端的 request.parameters 对象访问。

如果你有兴趣,这里有更多的细节:当您使用其中,方法,你传递将被转换成<一个部分模板对象href=\"http://www.odata.org/documentation/odata-v2-documentation/uri-conventions/#45_Filter_System_Query_Option_filter\">OData $过滤器前pression 传递到服务器。然后,服务器将解析,并转换成相应的SQL WHERE 子句将被发送到数据库。所以,你原来的code将发送类似于此的请求(空格将与 20%所取代,但在这里给他们留下为清楚起见):

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

当你找到了,你可以通过 getComponents 函数访问它,但在这一点上,你将不得不解析前pression自己检索比较的值。如果您要访问客户端传递一些值,正如我所说的最简单方法是将它们传递作为正常的查询字符串参数。在code我送,将发送将类似于

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

和那些你可以从 request.parameters 直接(该值的类型检索值将是字符串,所以如果你想将它们视为布尔值的数你需要做相应的转换)。

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).

这篇关于从HTML / JS应用程序传递查询参数的Azure服务器脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆