MooTools JSON 请求
尝试使用 MooTools Request.JSON 获得一个非常简单的请求。在从头开始构建它失败之后,我从某个地方拿了一个例子,慢慢地将其削减到最低限度,然后将其放回到我自己的页面中。唯一改变的是 url 和元素 ID,但没有任何效果。
任何帮助、想法,将不胜感激。
json.php
<?php
$result['name'] = 'yay';
header('Content-type: application/json');
echo json_encode($result);
?>
demo.js (window.addEvent('domready', function() { ) 内的
$(document.body).getElement('input[id=game_name]').addEvents({
'keydown' : function(){
alert('hmm'); //this fires
var jsonRequest = new Request.JSON({
url: "json.php",
onComplete: function(result){ //changing to onSuccess kills everything afterwards
alert('result.name'); //this fires
alert(result.name); //this does not fire
alert('result.name'); //this does not fire
}
}).get();
}
});
片段 。
再次感谢
,事实证明,问题是我不小心将同步的重复文件加载到浏览器中,因此(显然)无法在服务器端执行任何操作
非常感谢您的帮助 .
Trying to get a very simple request working with MooTools Request.JSON. After having no success building it from scratch, I took an example from somewhere and slowly pared it down to the bare, bare minimum, then put it back into my own page. The only things changed are the url and element ID, but to no avail.
Any help, ideas, will be greatly appreciated.
json.php
<?php
$result['name'] = 'yay';
header('Content-type: application/json');
echo json_encode($result);
?>
demo.js (snippet inside window.addEvent('domready', function() { )
$(document.body).getElement('input[id=game_name]').addEvents({
'keydown' : function(){
alert('hmm'); //this fires
var jsonRequest = new Request.JSON({
url: "json.php",
onComplete: function(result){ //changing to onSuccess kills everything afterwards
alert('result.name'); //this fires
alert(result.name); //this does not fire
alert('result.name'); //this does not fire
}
}).get();
}
});
PS. in neither my page, or the pared down example pages, can i get the request to send on domready, only inside an event. why is that?
thanks again
As it turns out, the problem was that I had accidentally loaded a synced duplicate file into my browser that was therefore (obviously) unable to execute anything server side.
Thank you very much for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
几个建议/问题:
您的网络浏览器控制台中是否出现任何错误?您使用哪种网络浏览器?第三个警报根本没有触发的事实表明
alert(result.name);
正在抛出错误,在这种情况下,所有进一步的执行都将停止,并且错误将出现在您的浏览器的控制台。当你说“更改为 onSuccess 会杀死之后的一切”时,你到底是什么意思?下面的代码(即上面代码片段中未包含的代码)是否永远不会执行?或者
onSuccess
永远不会触发?json.php
是否与运行此脚本的页面位于同一目录中?尝试将url: "json.php"
中的json.php
替换为绝对 URL(/mydirectory/json.php
或http ://www.mywebsite.com/mydirectory/json.php
)并查看这是否有效。如果有任何帮助,以下代码会导致显示“yay”的警报(在本地服务器上运行;
json.php
是包含您问题中的 PHP 代码的文件):Several suggestions/questions:
Are you getting any errors in your web browser's console? Which web browser are you using? The fact that the third alert doesn't fire at all suggests that
alert(result.name);
is throwing an error, in which case, all further execution will be stopped and an error will appear on your browser's console.When you say "changing to onSuccess kills everything afterwards", what exactly do you mean? Does code further down (i.e. code that's not included in the above code snippet) never execute? Or does
onSuccess
just never fire?Is
json.php
in the same directory as the page where this script is running? Try replacingjson.php
inurl: "json.php"
with an absolute URL (/mydirectory/json.php
orhttp://www.mywebsite.com/mydirectory/json.php
) and see whether this works.If it's any help, the following code results in an alert reading "yay" (running on a local server;
json.php
is a file containing the PHP code in your question):你可以在这里找到一个很棒的教程
http://net.tutsplus.com/tutorials/javascript-ajax/checking-username-availability-with-mootools-and-request-json/
you can find a great tutorial here
http://net.tutsplus.com/tutorials/javascript-ajax/checking-username-availability-with-mootools-and-request-json/
这里有完全相同的问题。
我通过解码 JSON 字符串解决了这个问题,该字符串作为参数(而不是预期的对象)给出。
这是文档:
http://mootools.net/docs/core/Utilities/JSON#JSON:解码
Exactly the same problem here.
I solved it by decoding the JSON string, which is given as parameter (instead of the expected object).
Here ist the documentation:
http://mootools.net/docs/core/Utilities/JSON#JSON:decode