简体   繁体   English

通过ajax返回大量的html(带有几个变量)?

[英]return a large amount of html (with a couple of variables) via ajax?

I have an ajax function search for keywords in a big database. 我有一个ajax函数来搜索大型数据库中的关键字。 The php being called simply says "no" if there's nothing, but if there are found records, it goes ahead and creates all of the HTML and returns the html, so that AJAX only needs to put the returned text into the html of a div. 如果什么都没有,被调用的php只是简单地说“ no”,但是如果找到记录,它将继续并创建所有HTML并返回html,因此AJAX只需要将返回的文本放入div的html中。 。 My problem is that I'd like to pass along a couple of variables, like the number of records found, etc. 我的问题是我想传递几个变量,例如找到的记录数等。

So if I tried to put it in a statement that javascript could eval, I'm afraid that not only is all of the html potentially big enough to cause some sort of variable problems, but it also has a lot of single and double quotes, etc, that could unexpectedly end the variable before it's supposed to. 因此,如果我尝试将其声明为javascript可以评估的话,恐怕不仅所有html都可能足以引起某种可变问题,而且它还有很多单引号和双引号,等等,这可能会意外地使变量在应有的情况下结束。 See the following 见以下

// (I know I don't have a single quote after data and that will break it. This is just an example
echo "{ status: 'success', total: '".count($relevance)."' data: ";
foreach ($relevance as $re) {        
    // tons of html is printed here
}
echo " }";

So the question is, how do I most effectively send back a whole gang of html code, along with some variables that can be easily eval'ed by JS? 所以问题是,我如何最有效地将整个html代码以及一些易于由JS评估的变量一起发送回去?

Use json_encode This will eliminate any errors you might have in trying to create your own json. 使用json_encode这将消除您尝试创建自己的json时可能遇到的任何错误。

$returnArray = array(
    'status'=>'success',
    'total' => count($relevance),
    'data' => ''
);

foreach ($relevance as $re) {        
    $returnArray['data'] .= $re; // + all long html code
}

echo json_encode($returnArray);

You can encode the data as JSON and then put it in a <script> block that's got a non-JavaScript type. 您可以将数据编码为JSON,然后将其放入具有非JavaScript类型的<script>块中。 Give the script a class too so your code can look for it easily. 也给脚本一个类,以便您的代码可以轻松地查找它。 You can then get the ".innerHTML" of the <script> element and decode the JSON. 然后,您可以获取<script>元素的“ .innerHTML”并解码JSON。 Then just add the <script> to the rest of the HTML you're returning. 然后只需将<script>添加到要返回的其余HTML中。

edit No use @Neal's answer instead; 编辑不要使用@Neal的答案; it's a less goofy idea. 这不是一个愚蠢的想法。 I've done what I've described but usually that's because for some other (framework) reason it's not easy (or just inconvenient) to get directly at the response data. 我已经完成了我所描述的操作,但是通常这是因为由于其他一些(框架)原因,要直接获得响应数据并不容易(或者很不方便)。 Also, I generally generate pages via JSP, so it's much easier to drop JSON into a page than to get the page contents into Java. 另外,我通常通过JSP生成页面,因此将JSON放到页面中比将页面内容放到Java中要容易得多。

To elaborate, a <script> block like this: 详细来说,如下所示的<script>块:

<script type='text/json' class='some-data-for-you'>
  { "hello": "world" }
</script>

will be ignored by the browser because the "type" won't be recognized as code. 将被浏览器忽略,因为“类型”不会被识别为代码。 Then your JavaScript code can just look for <script> elements with class "some-data-for-you" in the returned content and then parse the ".innerHTML" with a JSON parser. 然后,您的JavaScript代码可以在返回的内容中查找类为“ some-data-for-you”的<script>元素,然后使用JSON解析器解析“ .innerHTML”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM