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
分享到:
相关推荐
测试
在IT行业中,我们经常遇到各种错误,"failed to execute script"是其中之一,尤其在尝试运行Python脚本时。这个问题通常发生在使用Python的`py`或`exe`可执行文件时,尤其是在试图通过命令行传递参数时。让我们深入...
在使用Maven构建Java项目时,可能会遇到这样一个错误:“Failed to execute goal on project …: Could not resolve dependencies for project …”。这个错误通常表明Maven在构建过程中遇到了依赖解析问题,无法...
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
然而,在使用`pyinstaller`将Python脚本打包成exe文件后,可能会遇到“Failed to execute script pyi_rth_certifi”这样的错误。本文将详细解释这个问题的原因,并提供解决此问题的方法。 首先,`pyinstaller`在...
Jenkins构建Execute shell script on remote host using ssh命令;JenkinsSSH执行命令;
### React Native错误:Failed to Execute aapt 解决方案 #### 一、问题概述 在React Native项目开发过程中,可能会遇到“Failed to execute aapt”这一错误。该错误通常发生在使用命令行工具(cmd)启动项目时,...
关于解决“Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1”问题,首先需要了解Maven的构建生命周期,以及Maven的编译插件(maven-compiler-plugin)如何在编译过程中发挥作用。...
在<plugins></plugins>中添加 <groupId>org.apache.maven.plugins <artifactId>maven-compiler-plugin <version>3.8.1 <source>1.8 <target>1.8 </plugin>
相较于纯JavaScript库或者Ajax控件库,Ajax Part Taglib更加易于集成,并且可以无缝地与现有的Struts项目进行整合。此外,它还提供了一系列预定义的标签,用于简化Ajax功能的实现过程。 #### 简单的Hello World示例...
关于FlexSPI启动和调试的详细信息,可以在NXP官方文档AN12108《How to Enable Boot from QSPI Flash》和AN12183《How to Enable Debugging for FLEXSPI NOR Flash》中找到。此外,中国技术支持团队也提供了相关的...
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构建springboot项目自动部署的时候,配置远程执行Dockerfile所在服务器的SSH命令,这里把完整的命令贴出来,详细搭建过程见我的博客《基于Jenkins+Gitlab+Docker实现SpringBoot项目自动部署》
在这个场景中,我们将探讨如何使用纯JavaScript和AJAX(Asynchronous JavaScript and XML)来实现这一功能。AJAX允许我们在不刷新整个页面的情况下与服务器进行异步通信,提供更好的用户体验。 首先,我们需要一个...
在排除json数据格式不正确的原因之后,发现了ajax error函数返回“networkerror failed to execute ‘send’ on ‘xmlhttprequest’ failed to load ‘http //“ XMLHttpRequest.status=0,就是没有初始化。...
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 ...
TypeError: Failed to execute ‘observe’ on ‘MutationObserver’: parameter 1 is not of type ‘Node’ InfiniteScroll的更多用法element官网 二、解决办法 给需要使用 InfiniteScroll 的元素或者它的父级...
在精简pyinstaller打包后的exe时,出现 Failed to execute scripte app 问题。无法定位到具体问题。 使用命令 出现问题命令: pyinstaller -Fw app.py 然而,通过vscode运行,或者python app.py并没有报错,推测...
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/...