jquery $.post - 三部曲到此结束 - 关于 json 与 $.post 集成的一些问题
这是我在互联网上看到的一些代码,可以帮助我归档或多或少相同的内容:
<input size="30" id="inputString" onkeyup="lookup(this.value);" type="text" />
function lookup(inputString) {
if(inputString.length == 0) {
// Hide the suggestion box.
$('#suggestions').hide();
} else {
$.post("rpc.php", {queryString: ""+inputString+""}, function(data){
if(data.length >0) {
$('#suggestions').show();
$('#autoSuggestionsList').html(data);
}
});
}
} // lookup
1) ** {查询字符串:“”+输入字符串+“”} 这是一个 json 表示法,但为什么我们需要第一个“”和最后一个“”?**
2) $('#autoSuggestionsList').html(数据);
[更新] 这假设从服务器端返回的数据采用 html 格式。 如果该格式是 json,我们需要在这里修改什么以使其适应 json 服务器端响应? [/更新]
谢谢, MEM
Here's some code that I've seen on the internet to help me out archieving more or less the same:
<input size="30" id="inputString" onkeyup="lookup(this.value);" type="text" />
function lookup(inputString) {
if(inputString.length == 0) {
// Hide the suggestion box.
$('#suggestions').hide();
} else {
$.post("rpc.php", {queryString: ""+inputString+""}, function(data){
if(data.length >0) {
$('#suggestions').show();
$('#autoSuggestionsList').html(data);
}
});
}
} // lookup
1)
**
{queryString: ""+inputString+""}
It's a json notation but why do we need the first "" and the last "" ?**
2)
$('#autoSuggestionsList').html(data);
[UPDATE]
This assumes that the returned data from or server side, comes in html format.
What if, that format was in json, what do we need to modify here, to adapt it to json server side response?
[/UPDATE]
Thanks,
MEM
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要双引号来关闭字符串,然后添加变量,然后再次打开字符串
您可以使用 jquery 站点上的示例返回 json
You need the double quotes to close the string then add in the variable then open string again
You can get json back using something like the example on the jquery site
在回答1)时,你确定它的意思是“”(双引号,双引号)而不是“””(单引号,双引号,单引号)。
这会更有意义,因为你将会是 wuating 无论
inputString
是什么(例如,如果输入字符串是 'hello' 那么你的行将是queryString: "hello"
我认为上面发生的是你是在
inputString
中附加和添加空字符串我不完全确定您对 (2) 的要求,但一般来说,在隐藏输入中存储 JSON 数据是可接受的做法。您可以使用 JSON 解析器将其转换为 JavaScript 对象以供使用
:
如果你想看一下,我已经做了一个 jsfiddle 模型:
http://jsfiddle.net/gRQMy/5/
In answer to 1) are you sure its meant to be "" (double-quote, double-quote) and not '"' (single-quote, double-quote, single-quote).
This would make more sense, as you would be wuating whatever
inputString
is. (E.g. if input string is 'hello' then your line would bequeryString: "hello"
What I think is happening above is that you are appending and prepending an empty strings to
inputString
.I'm not entirely sure what you're asking with (2), but in general, the storage of JSON data in a hidden input is accepted practice. From there you can use a JSON parser to turn it into a JavaScript object for use.
Edit:
I've done a jsfiddle mockup if you want to have a look:
http://jsfiddle.net/gRQMy/5/