如何使用jquery发送int类型参数 [英] how to send int type parameters using jquery

查看:379
本文介绍了如何使用jquery发送int类型参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个使用jquery与网页通信的Web服务。
我想构建我的web服务,因此它是类型安全的,无需在服务器端执行转换。

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.

如何从中发出ajax调用客户端,使用jquery到期望int值参数的服务器。

How can I issue an ajax call from the client side, using jquery to the server that's expecting an int value parameter.

编辑:
我明白这是不可能的。我用c#编写服务器端。
目前,Web服务支持来自客户端(js)和其他实用程序(其他c#程序)的调用。我目前可以想到的最简单的解决方案是复制方法并将其签名更改为字符串,然后转换数据类型并调用方法,这次使用正确的数据类型。

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.

是否有任何.net 4属性可以装饰我自动执行此操作的方法?

Is there any .net 4 attribute that I can decorate my method that performs this automatically?

谢谢

推荐答案

我怕你不能。使用请求发送的所有参数都是string类型。

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

您可以将参数作为编码的JSON发送。

You could send the parameters as encoded JSON.

假设一个对象

{"int":1}

urlencoded它是

urlencoded it is

%7B%22int%22%3A1%7D

http://domain.org/params=%7B%22int%22%3A1%7D

解码它:

$params=json_decode($_GET['params']);

你会看到类型仍然是整数:

And you will see that the type is still integer:

echo gettype($params->int);

但这也是服务器端转换(因为你需要解码JSON)。

But this somehow also is a serverside conversion(because you need to decode the 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>

输出:

object(stdClass)#1 (4) {
  ["pig"]=>
  int(1)
  ["cow"]=>
  float(2.2)
  ["duck"]=>
  string(6) "donald"
  ["rabbit"]=>
  object(stdClass)#2 (1) {
    ["ears"]=>
    int(2)
  }
}

这还不错在我看来,这就是JSON被发明的原因,在应用程序之间交换数据。

That's not bad in my mind, that's simply what JSON has been "invented" for, interchange data between applications.

这篇关于如何使用jquery发送int类型参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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