CodeIgniter encountering a persistent issue with AJAX that causes it to reload the

Whenever I attempt to send an AJAX request with jQuery, the response I receive is the HTML of the same page! Here's a live preview (editing not available as I am currently fixing it)

Below are the files:

Main controller:

Class Main extends Controller {

function Main() 
{
      parent::Controller();   
}

function index(){

    $this->load->view('oxila_index');
}}

Oxila_index view (only the JS part, rest of the HTML is in the link above)

<script type="text/javascript">
        $(document).ready(function(){
            $("#inv").hide();
        });
        $(document).ready(function(){
            $("#submit").click(function(evt){

                $.post("/ajax/process", {
                    url: $("#url").val()
                }, function(response){
                        $("#output").html("");
                    $("#inv").show("slow"); 
                        $("#output").html(response);
                }, "text");
                evt.preventDefault();
            }); 
        });
    </script>

Ajax Controller

Class Ajax extends Controller {

    function process(){

        $data['url'] = $this->input->post('url');
        $this->load->view('test',$data);
        echo "hello world";
    }
}

Answer №1

After testing out your webpage, I found that it works well for me. Just so you know, I accessed it using Google Chrome on Linux.

I do have a couple of suggestions though. First off, consider moving the script from the <head> section to the bottom of the page, just above the </body> tag. This practice helps in preventing the blocking of concurrent loading of other page elements, CSS, and images while waiting for the JS code to load.

Secondly, if there are no changes required on the server side, opt for using GET instead of POST. Additionally, to avoid caching issues in Internet Explorer, simply include an extra parameter with a random value.

Finally, regarding the line $("#output").html("");, it may not be necessary. You could directly use $("#output").html(response); as the .html() function will replace any existing content within the container without having to clear it first.

Answer №2

Someone from the forum shared this solution with me

After typing different paths after the url, I kept getting redirected to the home page.

They suggested changing the URL Protocol in the config/config.php file from AUTO to something else.

I followed their advice and changed it to “REQUEST_URI” which fixed the issue!

Answer №3

Your Test Function does not include a view or model for handling the Post Request

Answer №4

Ensure that your configuration file includes the following setting as FALSE:

$config['compress_output'] = FALSE;

Answer №5

What is the purpose of a public function?

public function sampleFunction(){

    echo $this->input->post('url');

}

Consider removing the "public" access modifier to make it like any other function:

Additionally, try including the complete URL in JavaScript to see if it functions correctly:

$.post("http://yourwebsite/oxila/test/")

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Create a responsive Flexslider with a dynamic background image

Has anyone experienced issues with creating a responsive design while working with flexslider and background images? I tried placing contents inside li tags with background images, but it seems to break the slider's responsiveness. Here is the code sn ...

Upon inputting the text value, the concealed button will automatically appear without the need to press any buttons

I would like to create something similar. The hidden button should appear after entering the text value without the need to press any buttons. For example, if a user enters Hazrat Shahjalal International Airport, Dhaka (DAC), the button will become visibl ...

What is the best way to showcase a collection of data retrieved in JSON format from a server using Ajax

Struggling with sending a request to a PHP file that returns JSON data and facing issues where only one record is displayed with undefined values. Here is the JSON response received from core/backend/comm.php: { "name": "Rehan", "location": "Pune ...

Ensure the footer remains at the bottom of the page even with changing

I am encountering an issue with positioning a footer under dynamic content displayed in a div named txtresults. The div is updated with HTML following a query made with an input value, as shown in this example on JsFiddle: JsFiddle Currently, I am struggl ...

Implementing Materialize CSS functionality for deleting chips

I've been attempting to extract the tag of a deleted chip from the div within the Materialize chips class, but I'm hitting roadblocks. My failed attempts so far: $('.chips').on('chip.delete', function(e, chip){ console.lo ...

Automatically tapping the Quora "expand" button through code

I am attempting to automate the clicking of the "more" button located at the bottom of a page like using watir. Below is the code I currently have: require 'watir-webdriver' b = Watir::Browser.new b.goto 'quora.com/'+ ARGV[2] + ' ...

Get the PDF file and access it with Ajax technology

I am facing an issue with my action class that is responsible for generating a PDF. The code snippet shown sets the contentType appropriately. public class MyAction extends ActionSupport { public String execute() { ... ... File report = si ...

The jQuery pop-up fails to activate on the initial click

I have multiple "Buy Now" buttons for different products. If the button is labeled as "sold-out," it should not do anything, but if it's available, it should trigger a jQuery Magnific Popup. Currently, the popup only opens after the second click becau ...

Displaying a spinner within Bootstrap tabs to indicate content is loading via ajax

Using Bootstrap 3 tabs and AJAX to load tab content can sometimes result in slower loading times. To address this issue, it would be beneficial to incorporate a spinner within the tab content to indicate that the server is processing the request. Is there ...

Troubles with the compatibility of javascript and jquery's multiselect plugin

I've been utilizing the multiselect API to create a dropdown with multiple select options. This is my HTML code: <select id="options" multiple="multiple"></select> And this is my JS code: render:function(){ // $('#viewTemp& ...

Using jQuery to make a PNG image draggable and allow it to overlap with

Can jQuery's Draggable be used with an overlapping PNG image (not as a background image, but for printing purposes)? I attempted using the CSS style "pointer-events: none," however this solution does not function correctly in Internet Explorer. <d ...

Is there a different option than jQuery.ajaxSettings.traditional for use with XMLHttpRequest?

Is there a different option similar to jQuery.ajaxSettings.traditional for xmlhttprequest? or What is the method to serialize query parameters for an xmlhttprequest? ...

Retrieve the id of the clicked hyperlink and then send it to JQuery

<a class = "link" href="#" id = "one"> <div class="hidden_content" id = "secret_one" style = "display: none;"> <p>This information is confidential</p> </div> <a class = "link" href="#" id = "two" style = "display: non ...

What is the best way to input a dynamic post array in PHP?

Explaining this might be challenging, but I will do my best. I have multiple input posts with custom conditions, which means there can be as many custom conditions as needed. Here is the HTML: <input name="type[]" value="type 1"> <input nam ...

Making an Ajax GET request with an authorization header and CORS in Android version 2.3.3

On my Android 2.3.3 device, I'm trying to perform a cross-domain GET request using CORS Filter on the server. While it works fine on Chrome, Firefox, and iPhone, there seems to be an issue with Android 2.3.3. The preflight request appears to be correc ...

Exploring the Power of Ajax Integration with Mongodb

I have been trying to figure this out for a while now, but I'm lost. I am attempting to use Ajax to retrieve information from a collection named 'reizen' in MongoDB. My goal is to cycle through all elements within the collection and extract ...

I incorporated an AJAX-based page into the PrimeFaces tab, but unfortunately, it is not functioning correctly

I've added different pages within prime faces tabs and used ID-based forms on each page. However, when I enable AJAX for each page it doesn't work properly within the tab. Here is a snippet of my primary index.xhtml page: index.xhtml <?xml ...

Switching the displayed image depending on the JSON data received

As a beginner in javascript and jQuery, I am working on displaying JSON results in the browser. My goal is to generate dynamic HTML by incorporating the JSON data. Below is an example of the JSON structure: [{"JobName":"JobDoSomething","JobStatus":2,"JobS ...

Interactive Chart with Angular for Multiple Selections

I have a single page with a list menu By utilizing the list menu, I am able to generate various types of charts such as line chart, bar chart, pie chart, and more. However, I would like the previously selected charts, like the line chart, pie chart, etc. ...

Tips for maintaining interactivity after a page refresh

$(document).ready(function () { $("#2").click(function () { $("#content").load("{% url 'about' %}"); }); $("#3").click(function () { $("#content").load("{% url ...