如何使用jquery发送int类型参数

时间:2022-12-08 20:20:46

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服务。我想构建我的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

4 个解决方案

#1


7  

I'm afraid you can't. All parameters send with an request are of type string.

我怕你不能。使用请求发送的所有参数都是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被“发明”的原因,在应用程序之间交换数据。

#2


3  

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

#3


3  

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代码中的int,float或其他任何东西(使用parseInt(),parseFloat()...),但是当向服务器发送请求时,这会丢失 - 参数没有关于发送到服务器的数据类型的任何信息,因此它们都被视为字符串(并且,根据您的服务器,转换为接收时的预期值)

#4


1  

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发送的内容本身就是任何类型,而不是具体说明服务器上的结果是什么语言。您可以使用每个值发送类型参数,但您仍需要验证它们在服务器端实际匹配。

#1


7  

I'm afraid you can't. All parameters send with an request are of type string.

我怕你不能。使用请求发送的所有参数都是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被“发明”的原因,在应用程序之间交换数据。

#2


3  

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

#3


3  

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代码中的int,float或其他任何东西(使用parseInt(),parseFloat()...),但是当向服务器发送请求时,这会丢失 - 参数没有关于发送到服务器的数据类型的任何信息,因此它们都被视为字符串(并且,根据您的服务器,转换为接收时的预期值)

#4


1  

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发送的内容本身就是任何类型,而不是具体说明服务器上的结果是什么语言。您可以使用每个值发送类型参数,但您仍需要验证它们在服务器端实际匹配。