JSON保存在数据库中并使用JQuery加载
I create a huge JSON-Object and save it in my database. But when I load the "string" and echo it in PHP, I can't access the JSON Object in JQuery. Do I have to consider something if I want to save my JSON Object in a MySQL Database (when I just create the Array and then echo it with "echo json_encode($arr);" it works fine, but I need to save the Object for caching).
我创建了一个巨大的JSON-Object并将其保存在我的数据库中。但是当我加载“string”并在PHP中回显它时,我无法访问JQuery中的JSON对象。如果我想将我的JSON对象保存在MySQL数据库中(当我只是创建数组然后用“echo json_encode($ arr)”回显它时,我是否必须考虑一下;“它工作正常,但我需要保存对象用于缓存)。
{"247":{"0":"This is a question","1":"","2":"247","3":"0","answers":[["Answer1","960","1"],["Answer 2","962","0"],["Answer 3","961","0"],["Answer 4","963","0"]]},{"248":{"0":"This is a question","1":"","2":"247","3":"0","answers":[["Answer1","960","1"],["Answer 2","962","0"],["Answer 3","961","0"],["Answer 4","963","0"]]}}
{“247”:{“0”:“这是一个问题”,“1”:“”,“2”:“247”,“3”:“0”,“答案”:[[“Answer1”, “960”,“1”],[“答案2”,“962”,“0”],[“答案3”,“961”,“0”],[“答案4”,“963”,“ 0“]]},{”248“:{”0“:”这是一个问题“,”1“:”“,”2“:”247“,”3“:”0“,”答案“: [[“Answer1”,“960”,“1”],[“答案2”,“962”,“0”],[“答案3”,“961”,“0”],[“答案4” , “963”, “0”]]}}
just an excerpt
只是一个摘录
If I just echo this JSON-Object, everything works fine, but if I load the same string from the database and echo it, it doesn't work.
如果我只是回显这个JSON-Object,一切正常,但如果我从数据库加载相同的字符串并回显它,它就不起作用。
Update 1: forget to tell that I'm using a TEXT-Field with UTF8_general_ci collation
更新1:忘记告诉我我正在使用带有UTF8_general_ci排序规则的TEXT-Field
Update 2: Maybe a little bit more code:
更新2:也许更多代码:
function start() {
$(".start").click(function () {
$.post("load_script.php", { }, function(data){
alert(data[247][0]);
}, "json");
return false;
});
}
this loads the script and should alert "This is a question"
这会加载脚本,并应提醒“这是一个问题”
<?php
require_once('connect.php');
$ergebnis = mysql_query("SELECT text FROM cache_table ORDER BY RAND() LIMIT 1");
while($row = mysql_fetch_object($ergebnis)) {
$output = $row->text;
}
echo $output;
?>
this is the script, where I load the database entry with the JSON-Object.
这是脚本,我用JSON-Object加载数据库条目。
Update 3: I think I solved the problem. Some break sneaked into my JSON-Object so I do this, before the output:
更新3:我想我解决了这个问题。有些打破潜入我的JSON-Object所以我这样做,在输出之前:
$output = str_replace("\n", "", $output);
$output = str_replace("\r", "", $output);
$output = str_replace("\r\n", "", $output);
3 个解决方案
#1
3
I'd suggest looking at what your javascript is seeing. Instead of asking jQuery to interpret the json for you, have a look at the raw data:
我建议看看你的javascript看到了什么。不要让jQuery为你解释json,而是查看原始数据:
function start() {
$(".start").click(function () {
$.post("load_script.php", { }, function(data){
alert(data);
}, "text");
return false;
});
}
For example, if part of the string gets oddly encoded because of the UTF-8, this might cause it to appear.
例如,如果由于UTF-8而导致部分字符串被奇怪编码,则可能导致它出现。
Once you've done that, if you still can't spot the problem, try this code:
完成后,如果仍然无法发现问题,请尝试以下代码:
var data1, data2;
function start() {
$(".start").click(function () {
$.post("load_script.php", {src: "db" }, function(data){
data1 = data;
}, "text");
$.post("load_script.php", {src: "echo" }, function(data){
data2 = data;
}, "text");
if (data1 == data2) {
alert("data1 == data2");
}
else {
var len = data1.length < data2.length ? data1.length : data2.length;
for(i=0; i<len; ++i) {
if (data1.charAt(i) != data2.charAt(i)) {
alert("data1 first differs from data2 at character index " + i);
break;
}
}
}
return false;
});
}
And then change the PHP code to either return the data from the database or simply echo it, depending on the post parameters:
然后更改PHP代码以从数据库返回数据或简单地回显它,具体取决于post参数:
<?php
if ($_POST['src'] == 'db')) {
require_once('connect.php');
$ergebnis = mysql_query("SELECT text FROM cache_table ORDER BY RAND() LIMIT 1");
while($row = mysql_fetch_object($ergebnis)) {
$output = $row->text;
}
}
else {
$output = '{"247":{"0":"This is a question","1":"","2":"247","3":"0","answers":[["Answer1","960","1"],["Answer 2","962","0"],["Answer 3","961","0"],["Answer 4","963","0"]]},{"248":{"0":"This is a question","1":"","2":"247","3":"0","answers":[["Answer1","960","1"],["Answer 2","962","0"],["Answer 3","961","0"],["Answer 4","963","0"]]}}';
}
echo $output;
?>
Hope that helps!
希望有所帮助!
更多相关文章
- zTree.js 异步加载地区例子
- 加载外部站点并更改其可视化
- Emberjs应用程序加载除Index之外的所有路由
- javascript阻塞加载问题【转】
- 当页面上有多个按钮时,按钮样式在页面加载上有厚的边框
- 如何从Vue.js中的变量名加载组件?
- iOS uiwebview无法从javascript方法加载资源
- 执行javascript作为coldfusion页面加载
- jqGrid 数据加载完事件 loadComplete 使用