Ajax 返回 JSON 实现吗?
我有一个 codeigniter 应用程序,我正在对其进行一些更改。
我要做的第一个更改是允许管理员用户通过管理面板下订单,他们通过杂志、展览等接收订单。
因此,我将一个表单链接到一个 ajax 请求,从数据库中提取项目信息,然后然后将其编码为 JSON,然后将其返回到我的视图。
我遇到的问题是以良好的格式向用户显示它。
理想情况下,我想展示的是,
date.name
data.price
data.description
我需要用表单向他们展示这些详细信息,因为他们将在表单字段中输入数量,但我需要动态生成此信息和 HTML,这可能吗?或者我最好构建一个视图,然后通过 ajax 返回该视图?
我当前的ajax请求,
$.ajax({
url : '<?php echo current_url(); ?>',
data : "txtKeywords="+$("#txtKeywords").val()+"&search=Search For Item",
type : 'POST',
dataType: 'JSON',
success : function(data)
{
//alert(data);
}
});
I have a codeigniter application, and I am making some changes to it.
The first change I am doing is to allow admin users to place orders through the admin panel, where they have receieved orders through magazines, exhibitions etc.
So I have linked a form up an ajax request the pulls the item info back from the database and then encodes it into JSON and then returns it to my view.
What I am having trouble with is display this to the user in nice format.
Ideally I want to show,
date.name
data.price
data.description
I need to show them these details with a form as they are going to enter a quantity into a form field but I need to generate this information and HTML on the fly, is this possible? Or would I be best building a view and then returning that via ajax?
My current ajax request,
$.ajax({
url : '<?php echo current_url(); ?>',
data : "txtKeywords="+$("#txtKeywords").val()+"&search=Search For Item",
type : 'POST',
dataType: 'JSON',
success : function(data)
{
//alert(data);
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将视图添加到您的页面,如下所示:
将 Mustache 和 Mustache jquery 插件添加到您的页面: https:// github.com/janl/mustache.js/
现在,在成功回调中,您所做的
就是这样。 Mustache 使用 json 调用返回的数据渲染视图,然后代码将其附加到正文。
Add the view to your page like this:
Add mustache and the mustache jquery plugin to your page: https://github.com/janl/mustache.js/
Now in the success callback you do
That's it. Mustache renders the view with the data returned by the json call and then the code appends it to the body.