Attaching JSON to Web Page Request
I have a web page which I want to send some JSON data down with. Currently, I put necessary information within hidden input tags, then I make a call to a webservice to re开发者_JAVA百科trieve extra data. I would like to know what ways are available to merge the calls together.
Not sure what you mean about merging the calls together, but I don't see any reason for putting JSON data in hidden form fields. I'd just put it in a script
tag as a JavaScript literal:
<script type='text/javascript'>
var myJSONData = {
// ....
};
</script>
Since JSON is a subset of JavaScript object literal syntax, you can safely assume valid JSON is a valid JavaScript object literal.
For example, if your JSON is:
{ "Towel": "Always know where it is", "Answer": 42, "Author": "Douglas Adams"}
Then this is what the script
tag would look like:
<script type='text/javascript'>
var myJSONData = { "Towel": "Always know where it is", "Answer": 42, "Author": "Douglas Adams"};
</script>
How do you mean 'merge', do you intend to send your pre existing json to the webservice to get results?
If so, you could use a callback in the onload event to achieve the appearance of a single 'call'.
精彩评论