[英]script doesn't load after ajax response
I was trying to make an ajax call and show an html part inside a div class. 我试图进行ajax调用并在div类中显示html部分。 For that i used the following way.
为此,我使用了以下方式。
$.ajax({
type: "get",
url: "{{url('searchByCheckbox')}}",
dataType: 'html',
success: function(html)
{
$(".infinite-scroll").html(html)
}
});
But the problem is there is a script inside that html part which i wanted to load when i make first ajax call it's not loaded but for the second one it's loaded the script. 但是问题是在html部分中有一个脚本,当我进行第一个ajax调用时,我想加载该脚本,但第二个它已加载了脚本。 suppose the html response like this :
假设这样的html响应:
<script>
alert()
</script>
// html
How do i make it work? 我该如何运作? I put the script above the html page which i'm getting as response.
我将脚本放在html页面上方,作为响应。
(those who are marking the Question as duplicate should read at least what i want and what they wanted ) (那些将问题标记为重复的人应至少阅读我想要的内容和他们想要的内容)
Im sure that the error is happening because of the script, because you are closing the </script>
tag inside the html. 我肯定由于脚本而发生错误,因为您正在关闭html内的
</script>
标签。
The best solution is to return the data from your ajax
call as a json
最好的解决方案是将
ajax
调用中的数据作为json
To do that you should: 为此,您应该:
1- add a dataType
to your ajax
parameter as the below: 1-如下所示将
dataType
添加到您的ajax
参数:
$.ajax({
type: "get",
dataType: "json",
2- In the php
file handling the ajax
call, you must resurn the values as a json as below: 2-在处理
ajax
调用的php
文件中,必须将值作为json重新发送,如下所示:
Assume that currently you are doing the following: 假设当前您正在执行以下操作:
echo $html
You should change it to match the below: 您应该更改它以匹配以下内容:
$retArr = array(
"html" => $html, //Your html code without the javascript function
"jsFunc" => $jsFunc //Your java script function parameters
) ;
echo json_encode($retArr) ;
And then your ajax success
must be as the below: 然后您的
ajax success
必须如下所示:
success: function(data)
{ //You can access all the object parametes by calling the object name `data` followed by a dot `.` and then by the parameter key
data.jsFunc // Your javascript function params
$(".infinite-scroll").html(data.html) ;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.