2011-04-20 36 views
1

我对这个脚本感兴趣http://www.9lessons.info/2009/06/comment-system-with-jquery-ajax-and-php.htmljquery消息系统没有php如何?

我看到阿贾克斯调用commentajax.php

我想要做的就是忽略那个php,因为我想发布到一个json文件,然后从同一个文件得到响应。

我的服务器将使用POSTPUT把数据库中的数据,所以没有必要对我来说,使用PHP,只是语法是我的命:)

我想使用:

$.ajax({ 
type: "POST", 
url: "http://www.xxx.com/json", 
data: dataString, 
cache: false, 
success: function(html){ 
    $("ol#update").append(html); 
    $("ol#update li:last").fadeIn("slow"); 
    document.getElementById('comment').value=''; 
    $("#name").focus(); 
    $("#flash").hide(); 
} 
}); 

但那么commentajax.php会是什么样子? 可能与更换PHP:

$.getJSON('http://www.xxx.com/json' , function(data) { ... }); 

任何想法有助于 感谢。

EDIT1:i have the server-side script in place

+4

你确实认识到这绝对不能没有一些服务器端代码,对不对? – mattsven 2011-04-20 20:24:20

+0

我已经设置了 – Patrioticcow 2011-04-20 20:35:59

+0

那么,为什么不呢。你可以设置你的web服务器来允许'PUT'写请求到一个静态'json'文件。问题在于你没有针对流氓客户的缓解措施。你的jQuery代码需要GET文件,更新它,然后把它重新放回到json文件中。技术上可行,但有用性值得商榷。 – mario 2011-04-20 20:36:28

回答

0

如果您已经设置了服务器端脚本,那又有什么问题?

如果你问的是如何处理ajax调用,那么它主要是通过循环遍历JSON来返回,并以某种方式将这些值应用到网站。伪代码:

$.getJSON('http://www.xxx.com/json' , function(data) { 
for(i=0; i<data.comment.length; i++) { 
    $(".commentTitle").html(data.comment[i].title); 
    $(".commentBody").html(data.comment[i].text); 
} 
}); 
0

如果我这个正确阅读:

because i want to post to a json file and then get the response from the same file. 

你将需要一些服务器端脚本,以“后”的JSON文件。你如何获取数据到文件中。

您可以从服务器读取数据文件,这不是问题,而是将数据获取到需要服务器端脚本的文件中。

+0

我有服务器端脚本,我只需要知道什么是js的赖特语法 – Patrioticcow 2011-04-20 20:36:48