问题
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.
How can I issue an ajax call from the client side, using jquery to the server that's expecting an int value parameter.
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.
Is there any .net 4 attribute that I can decorate my method that performs this automatically?
Thank you
回答1:
I'm afraid you can't. All parameters send with an request are of type string.
You could send the parameters as encoded JSON.
Assuming an object
{"int":1}
urlencoded it is
%7B%22int%22%3A1%7D
Send a request to http://domain.org/params=%7B%22int%22%3A1%7D
on domain.org decode it:
$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).
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.
回答2:
You still should convert all values to int on the server, for security reasons.
From client, just pass ints, like script.php?id=2
$.get('script.php?id=2', function(data) {
$('.result').html(data);
alert('Load was performed.');
});
回答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)
回答4:
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.
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.
来源:https://stackoverflow.com/questions/4745627/how-to-send-int-type-parameters-using-jquery