Ajax is a nice technology to develop more interactive web pages and enhance user experience. with Ajax you can send a request to your web server with JavaScript and get the response, and show it on your page without refreshing the page .The popular problem for developer regarding to use of Ajax is when you need to some JavaScript in your web server response to be run. for example when you want to check the user input in a form's field and return a message by JavaScript alert function if its wrong, but because of that JavaScript code just import or innerHTML to your page like some text it doesn't not not actually execute to show an alert . the solution to this problem is eval() function which can execute the JavaScript code given to it as argument.
But another problem can be when your response is mixed with HTML and JavaScript together. In that case you have to parse the response first and poll out the JavaScript code out of it and run it. But make sure to have tags at beginning and end of you JavaScript.
function PaseAjaxResponse(somemixedcode)
{
var source = somemixedcode;
var scripts = new Array();
while(source.indexOf("<script") > -1 || source.indexOf("</script") > -1) {
var s = source.indexOf("<script");
var s_e = source.indexOf(">", s);
var e = source.indexOf("</script", s);
var e_e = source.indexOf(">", e);
scripts.push(source.substring(s_e+1, e));
source = source.substring(0, s) + source.substring(e_e+1);
}
for(var x=0; x<scripts.length; x++) {
try {
eval(scripts[x]);
}
catch(ex) {
}
}
return source;
}
This function first check the response and poll out all the and then in a while loop execute the script by eval() function one by one . and here is an exmple of how to use it with Ajax in Jquery
function doAjaxPost(divname, mydata) {
$.ajax({
type: \"POST\",
url: \"ajax.php\",
cache: false,
data: mydata,
success: function(msg){
document.getElementById(divname).innerHTML = msg;
PaseAjaxResponse(msg);
}
});
}
This function get a divname which is id of DIV you want to show the response there and mydata is the request ( like : ?show&code=12) you want to post to ajax.php page
http://www.yasha.co/Ajax/execute-javascript-on-Ajax-return/article-2.html
分享到:
相关推荐
failed to execute script 解决方案+命令行执行传参exe
测试
[INFO] ———————————————————————— [INFO] Reactor Summary for demo 0.0.1-SNAPSHOT: [INFO] [INFO] demo ……………………………………….. FAILURE [18:18 min] [INFO] admin-service ...
Python 3.85 使用pyinstaller在win10 x64下打包的应用程序,在win10 x64下运行完全正常,但是在win7 x64下则报错。failed to execute script pyi_rth_ pyi_rth_xxx 解决方法
Failed to execute goal org.apache.maven.plugins:maven-clean-plugin:2.5:clean (default-clean) on project
Python 使用pyinstaller打包成exe文件后运行出现Failed to execute script ***的解决方案
react-naitve 错误之Failed to execute aapt , react-native错误解决方案,
This updated edition of Java in a Nutshell not only helps experienced Java programmers ...Use Nashorn to execute JavaScript on the Java Virtual Machine Become familiar with development tools in OpenJDK
Jenkins构建Execute shell script on remote host using ssh命令;JenkinsSSH执行命令;
Learn how to execute web application penetration testing from end to end Key Features Build an end to end threat model landscape for Web Application Security Gain hands-on experience of using tools ...
在精简pyinstaller打包后的exe时,出现 Failed to execute scripte app 问题。无法定位到具体问题。 使用命令 出现问题命令: pyinstaller -Fw app.py 然而,通过vscode运行,或者python app.py并没有报错,推测...
This How-To guide is full of hands-on examples to get your databases automated in no time, with easy to follow code that will prove useful time and time again. Who this book is for If you are a ...
We would not only explore the IoT solution stack, but we will also see how to do it with the world’s most misunderstood programming language - JavaScript. Using Raspberry Pi 3 and JavaScript (ES5/...
在Jenkins构建springboot项目自动部署的时候,配置远程执行Dockerfile所在服务器的SSH命令,这里把完整的命令贴出来,详细搭建过程见我的博客《基于Jenkins+Gitlab+Docker实现SpringBoot项目自动部署》
You'll begin by learning how to establish build processes that are appropriate for JavaScript-driven development. Then, you'll walk through best practices for productive day-to-day development, like ...
You'll execute tasks whenever your code changes, run tests on every commit, and deploy in an automated fashion. Then you'll focus on designing modular components and composing them together to build ...
You can use this tool to simulate to execute SQL instead of PL-SQL(Oracle) and T-SQL(SQL server). Good SQL practise tool!
You will go on to understand how to use third-party Grunt and then create your own Grunt tasks that cater to your particular needs. This book first demonstrates various Grunt use cases before ...
主要介绍了Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1的解决办法的相关资料,需要的朋友可以参考下
在<plugins></plugins>中添加 <groupId>org.apache.maven.plugins <artifactId>maven-compiler-plugin <version>3.8.1 <source>1.8 <target>1.8 </plugin>