简体   繁体   English

在Codeigniter中纠正Ajax的好方法?

[英]Good way to right ajax in Codeigniter?

I was wondering around for a good way to write ajax in CodeIgniter. 我想知道一种在CodeIgniter中编写Ajax的好方法。 Is it ok to write ajax in views? 在视图中编写Ajax可以吗? or is it ok to make a new directory on the root of CodeIgniter project folder with the name of assets and move all the script in js folder that is within our assets folder. 或者可以在CodeIgniter项目文件夹的根目录下使用资产名称创建一个新目录,然后将所有脚本移动到我们资产文件夹内的js文件夹中。 If i put my script in assets folder than what to write in $.ajax url: ???????. 如果我将脚本放到assets文件夹中而不是在$ .ajax url中写什么:???????。

It is a good practice to keep all the js related stuff in separate js file. 最好将所有与js相关的内容保存在单独的js文件中。 It will be useful for minify the js files. 这对于缩小js文件很有用。

In case of "url" of $.ajax you can specify one base_url variable while loading you project and make that variable accessible in all files. 如果$ .ajax为“ url”,则可以在加载项目时指定一个base_url变量,并使该变量可在所有文件中访问。 In your js file use that base_url variable to specify the ajax url 在您的js文件中,使用该base_url变量指定ajax url

I personaly have it seperated, I also make sure that I have seperated ajax requests. 我个人认为它是分开的,我还确保我已经分开了ajax请求。 I have specific .JS files for all kinds of ajax requests, posts, gets, post with response, get with response and all that. 我为各种ajax请求,帖子,获取,带有响应的帖子,带有响应的获取以及所有这些都有特定的.JS文件。

Writing it in the view is actually no problem as long as you can work with it. 只要可以使用它,在视图中编写它实际上就没有问题。 At the end it's only extra work for the server to get an extra file anyway. 最后,无论如何,服务器获取额外的文件只是额外的工作。 Surely it's cleaner though! 当然,它更干净!

What I'd recommend just for DEV I would do it in the view, before it goes live I put it in a seperate file. 我只推荐给DEV的建议是,我会在视图中进行操作,然后再将其放入单独的文件中。

 //If your are writting ajax request into view or asset section of your root directory then ajax url should be look like below format //Pass a base url of a website from footer or view section into js file //Footer or View section or put your base_url as hard coded var base_url = '<?php echo base_url(); ?>'; //Url url: base_url + "index.php/<your-controller-name>/<your-method-name>/" 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM