简单的ajax/原型问题
我从 Ajax 开始,在包含 Ajax 文件时遇到问题。 在原始页面(如index.php)中编写并放置在(head)部分中的Ajax代码工作正常,但是当我尝试将代码放置在外部文件(在js文件夹中) ,prototype.js 文件放置在哪里),我没有得到任何响应,甚至在 Firefox 错误控制台中也没有得到任何响应。
除了调用 PHP 函数的 url 之外,我没有更改 Ajax 代码。
编辑:
调用ajax文件:
<script type="text/javascript" src="js/prototype.js"></script>
<script type="text/javascript" src="js/myValidation.js"></script>
</head><body>
....
Username: <input type="text" name="uname" id='uname' />
<a href="JavaScript:Validate();"> Available?</a>
<span id="result"></span>
Email address: <input type="text" name="email" />
...
我在html中嵌入了这个函数调用。验证函数来自书“PHP and Script.aculo.us Web 2.0 应用程序接口”
myValidation.js
function Validate(){
var user=$('uname');
var name="uname="+user.value;
var pars=name;
new Ajax.Request(
'myValidation.php',
{
method:'post', parameters:pars, asynchronous:true, onComplete: showAvailable
}
);
}
function showAvailable(originalRequest){
var newData=originalRequest.responseText;
$('result').innerHTML=newData;
}
这个例子来自提到的书
im beginning with Ajax, i have problem with including Ajax files.
Ajax code written in original page (like index.php) and placed in (head) section works fine, but when i try to place code in external file (in js folder, where is placed prototype.js file), i don't get any response, not even in Firefox Error Console.
I haven't changed Ajax code except url for calling PHP function.
edit:
calling ajax files:
<script type="text/javascript" src="js/prototype.js"></script>
<script type="text/javascript" src="js/myValidation.js"></script>
</head><body>
....
Username: <input type="text" name="uname" id='uname' />
<a href="JavaScript:Validate();"> Available?</a>
<span id="result"></span>
Email address: <input type="text" name="email" />
...
I embaded this function call in html. Validate function is from book "PHP and Script.aculo.us Web 2.0 app interfaces"
myValidation.js
function Validate(){
var user=$('uname');
var name="uname="+user.value;
var pars=name;
new Ajax.Request(
'myValidation.php',
{
method:'post', parameters:pars, asynchronous:true, onComplete: showAvailable
}
);
}
function showAvailable(originalRequest){
var newData=originalRequest.responseText;
$('result').innerHTML=newData;
}
This example is from mentioned book
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您还没有向我们展示您的
myValidation.js
文件,但以下是我看到的当人们从内联script
块移动到外部文件并且事情停止工作时的典型原因:script
块放入外部 JavaScript 文件中。你可能没有这样做,但我经常看到它,所以不得不提一下。您的外部脚本是纯 JavaScript,因此它应该是:不是:
我已经见过很多后者了。
他们将 JavaScript 文件放在与其脚本标记
src
不匹配的位置。他们在脚本中留下了开头
。重要的是不要这样做,在外部 JavaScript 文件中,这些是语法错误。
他们使用区分大小写的 Web 服务器,并且
src
属性和文件的实际名称不匹配。他们正在使用对权限敏感的 Web 服务器,并且该文件没有正确的权限。
对于上面最后两个例子,很容易检查:只需打开一个新选项卡并实际输入 JavaScript 文件的 URL。如果您看到 JavaScript,那就太好了;如果没有,您可能有更多信息。
对于这样的问题(以及数百个其他问题),没有什么比拥有一个像样的工具集更好的了。对于在浏览器上调试 JavaScript,有很多。有 Firebug(Firefox 插件)、Chrome 和 Safari 的开发工具(内置于浏览器中)、Microsoft Visual Studio 或用于使用 IE 进行调试的脚本调试器等。Firebug 和开发工具都会告诉您有关损坏的
src< /code> 链接,以及任何异常等。
You haven't shown us your
myValidation.js
file, but here are the typical reasons I see when people move from inlinescript
blocks to external files and things stop working:They put
script
blocks in the external JavaScript files. You probably didn't do that, but I've seen it often enough to mention it. Your external script is pure JavaScript, so for instance it should be:not:
I've seen the latter a fair bit.
They put the JavaScript file in a location that doesn't match their script tag
src
.They left an opening
<!--
or closing-->
in the script. Important not to do that, in external JavaScript files those are syntax errors.They're using a web server that's case sensitive and the
src
attribute and the file's actual name don't match.They're using a web server sensitive to permissions and the file doesn't have the right permissions.
In the case of the last two above, it's easy to check: Just open a new tab and actually enter the URL of the JavaScript file. If you see the JavaScript, great; if not, you probably have more information.
For issues like this (and hundreds of others), there's nothing like having a decent toolset. For debugging JavaScript on browsers, there are quite a few. There's Firebug (a Firefox add-in), Chrome's and Safari's Development Tools (built into the browsers), Microsoft Visual Studio or Script Debugger for debugging with IE, etc. Firebug and Dev Tools would both tell you about broken
src
links, as well as any exceptions, etc.您是否检查过这些文件是否可以通过 HTML 代码访问?还有更多 - 您是否将脚本放置在页面底部 - 因为 AJAX 只会将其处理程序绑定到现有元素?
Have you checked that those files are accessible from the HTML code? And more - have you placed you scripts in the bottom of the page - because AJAX will bind it's handlers only to existing elements?
问题解决了。
在 /js/ 文件夹中,我有一个 php 文件,我将其放在那里只是为了简单。将其移动到其他位置后一切正常。不知道这是否是规则,但 /js/ 文件夹中没有 php 文件。谢谢 TJ 和托马斯
Problem solved.
In /js/ folder i had one php file, that i put there just because of simplicity. After moving it to other location all worked. Don't know if that is rule, nut no php files in /js/ folder. Thanks T.J and Tomasz