jQuery 如何使用jquery发送int类型参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4745627/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to send int type parameters using jquery
提问by vondip
I am building a web service that will communicate with a web page using jquery. I want to build my webservice so it is type safe, without the need to perform conversions on the server side.
我正在构建一个将使用 jquery 与网页进行通信的 Web 服务。我想构建我的网络服务,使其类型安全,无需在服务器端执行转换。
How can I issue an ajax call from the client side, using jquery to the server that's expecting an int value parameter.
如何从客户端发出 ajax 调用,使用 jquery 到需要 int 值参数的服务器。
Edit: I understood that this is not possible. I am programming the server side with c#. currently the web service supports both calls from client side (js) and other utilities (other c# programs). The simplest solution I can currently think of is copying the methods and change their signature to string, then convert the datatypes and call the methods, this time with the correct data types.
编辑:我明白这是不可能的。我正在使用 c# 对服务器端进行编程。当前,Web 服务支持来自客户端 (js) 和其他实用程序(其他 c# 程序)的调用。我目前能想到的最简单的解决方案是复制方法并将它们的签名更改为字符串,然后转换数据类型并调用方法,这次使用正确的数据类型。
Is there any .net 4 attribute that I can decorate my method that performs this automatically?
是否有任何 .net 4 属性可以装饰自动执行此操作的方法?
Thank you
谢谢
回答by Dr.Molle
I'm afraid you can't. All parameters send with an request are of type string.
恐怕你不能。随请求发送的所有参数都是字符串类型。
You could send the parameters as encoded JSON.
您可以将参数作为编码的 JSON 发送。
Assuming an object
假设一个对象
{"int":1}
urlencoded it is
urlencoded 它是
%7B%22int%22%3A1%7D
Send a request to http://domain.org/params=%7B%22int%22%3A1%7D
向http://domain.org/params=%7B%22int%22%3A1%7D发送请求
on domain.org decode it:
在 domain.org 上解码它:
$params=json_decode($_GET['params']);
And you will see that the type is still integer:
你会看到类型仍然是整数:
echo gettype($params->int);
But this somehow also is a serverside conversion(because you need to decode the JSON).
但这在某种程度上也是服务器端转换(因为您需要解码 JSON)。
Regarding to the comment beyond, here an example that shows that it's not a pig with lipstick, try it and see if the types are preserved:
关于后面的评论,这里有一个例子表明它不是一只涂有口红的猪,试试看是否保留了类型:
<html>
<head>
<title>Test</title>
<script type="text/javascript">
<!--
function request()
{
var obj={
"pig":1,
"cow":2.2,
"duck":'donald',
"rabbit":{"ears":2}
};
location.replace('?params='+encodeURIComponent(JSON.stringify(obj)));
}
//-->
</script>
</head>
<body>
<input type="button" onclick="request()" value="click">
<pre><?php
if(isset($_GET['params']) && $params=json_decode($_GET['params']))
{
var_dump($params);
}
?>
</pre>
</body>
</html>
Output:
输出:
object(stdClass)#1 (4) {
["pig"]=>
int(1)
["cow"]=>
float(2.2)
["duck"]=>
string(6) "donald"
["rabbit"]=>
object(stdClass)#2 (1) {
["ears"]=>
int(2)
}
}
That's not bad in my mind, that's simply what JSON has been "invented" for, interchange data between applications.
这在我看来还不错,这只是 JSON 被“发明”的目的,用于在应用程序之间交换数据。
回答by simon
You still should convert all values to int on the server, for security reasons.
出于安全原因,您仍然应该在服务器上将所有值转换为 int 。
From client, just pass ints, like script.php?id=2
从客户端,只需传递整数,例如 script.php?id=2
$.get('script.php?id=2', function(data) {
$('.result').html(data);
alert('Load was performed.');
});
回答by oezi
you should never trust client data, so you'll have to check all values on serverside anyway - not doing this will cause security holes.
您永远不应该信任客户端数据,因此无论如何您都必须检查服务器端的所有值 - 不这样做会导致安全漏洞。
in addition, it's senseless anyway, because you can treat your values as ints, floats or whatever in your JS-code (using parseInt(), parseFloat() ...), but when sending a request to the server, this gets lost - parameters don't have any information about their datatype sent to the server, so they're all treated as strings (and, depending on your server, converted to what is expected when received)
此外,无论如何它都是毫无意义的,因为您可以将您的值视为整数、浮点数或 JS 代码中的任何内容(使用 parseInt()、parseFloat() ...),但是当向服务器发送请求时,这会丢失- 参数没有关于发送到服务器的数据类型的任何信息,因此它们都被视为字符串(并且,根据您的服务器,在接收时转换为预期的内容)
回答by markquezada
This will depend on what language you're using on the server side. Some languages will automatically type cast, others won't. For example PHP will convert something like ?id=1 to type int, whereas I believe ruby will keep it a string value unless you explicitly cast it to an int.
这将取决于您在服务器端使用的语言。有些语言会自动进行类型转换,有些则不会。例如,PHP 会将诸如 ?id=1 之类的内容转换为 int 类型,而我相信 ruby 会将其保留为字符串值,除非您明确将其转换为 int。
Unfortunately, you can't guarantee that something sent from javascript is any sort of type on its own, without being specific about what language is expecting the results on the server. You could send type parameters with each value, but you'd still have to verify that they actually match on the server side.
不幸的是,您不能保证从 javascript 发送的内容本身是任何类型的,而不具体说明期望服务器上的结果的语言。您可以使用每个值发送类型参数,但您仍然必须验证它们在服务器端是否实际匹配。