MooTools JSON 请求

发布于 2024-08-18 13:47:55 字数 950 浏览 8 评论 0原文

尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梨涡少年 2024-08-25 13:47:55

几个建议/问题:

  1. 您的网络浏览器控制台中是否出现任何错误?您使用哪种网络浏览器?第三个警报根本没有触发的事实表明 alert(result.name); 正在抛出错误,在这种情况下,所有进一步的执行都将停止,并且错误将出现在您的浏览器的控制台。

  2. 当你说“更改为 onSuccess 会杀死之后的一切”时,你到底是什么意思?下面的代码(即上面代码片段中未包含的代码)是否永远不会执行?或者 onSuccess 永远不会触发?

  3. json.php 是否与运行此脚本的页面位于同一目录中?尝试将 url: "json.php" 中的 json.php 替换为绝对 URL(/mydirectory/json.phphttp ://www.mywebsite.com/mydirectory/json.php)并查看这是否有效。

如果有任何帮助,以下代码会导致显示“yay”的警报(在本地服务器上运行;json.php 是包含您问题中的 PHP 代码的文件):

var jsonRequest = new Request.JSON({
    url: "json.php",
    onSuccess: function(result) {
        alert(result.name);
    }
}).get();

Several suggestions/questions:

  1. 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.

  2. 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?

  3. Is json.php in the same directory as the page where this script is running? Try replacing json.php in url: "json.php" with an absolute URL (/mydirectory/json.php or http://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):

var jsonRequest = new Request.JSON({
    url: "json.php",
    onSuccess: function(result) {
        alert(result.name);
    }
}).get();
诺曦 2024-08-25 13:47:55

这里有完全相同的问题。
我通过解码 JSON 字符串解决了这个问题,该字符串作为参数(而不是预期的对象)给出。

onSuccess: function(jsonString) {
    console.log(JSON.decode(jsonString));
}

这是文档:
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).

onSuccess: function(jsonString) {
    console.log(JSON.decode(jsonString));
}

Here ist the documentation:
http://mootools.net/docs/core/Utilities/JSON#JSON:decode

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文