简体   繁体   中英

Good way to right ajax in Codeigniter?

I was wondering around for a good way to write ajax in CodeIgniter. Is it ok to write ajax in views? 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. If i put my script in assets folder than what to write in $.ajax url: ???????.

It is a good practice to keep all the js related stuff in separate js file. It will be useful for minify the js files.

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. In your js file use that base_url variable to specify the ajax url

I personaly have it seperated, I also make sure that I have seperated ajax requests. I have specific .JS files for all kinds of ajax requests, posts, gets, post with response, get with response and all that.

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.

 //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>/" 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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