skip to Main Content

I am using the following JavaScript function to fetch the data using ajax call

function findName() {
    var name = "Jhon";
    $.ajax({
        method: "POST",
        url: "oc-content/themes/bender/ajax-test.php",
        data: { name : name },
        success: function (data) {
            alert(data);
        },
    })
}

It calls the following php file and works fine.

http://127.0.0.1/osclass/oc-content/themes/bender/ajax-test.php

But when I enable SEO friendly Permalinks in my CMS current page URL is appended in start of link and I get the following error in Chrome Console.

GET http://127.0.0.1/osclass/fashion-beauty/oc-content/themes/bender/ajax-test.php?name=Jhon 404 (Not Found)

Anybody tell me how to solve this issue?

3

Answers


  1. The url you’ve provided in the ajax call is document relative. When you changed the server’s url generation scheme, you also caused the url pointed at by the ajax call to change.

    Adjust the ajax url, changing:

    url: "oc-content/themes/bender/ajax-test.php",
    

    To:

    url: "/osclass/oc-content/themes/bender/ajax-test.php",
    
    Login or Signup to reply.
  2. Why don’t you make the URL server-relative? Something like this:

    function findName() {
        var name = "Jhon";
        $.ajax({
            method: "POST",
            url: "/osclass/oc-content/themes/bender/ajax-test.php",
            data: { name : name },
            success: function (data) {
                alert(data);
            },
        })
    }
    
    Login or Signup to reply.
  3. As you have not posted the php code. I would mention that any url directly navigated through addressbar of browser causes in the GET request and i can see you have a POST request in the ajax, so, it can’t work.

    Workaround would be to use $_REQUEST super globals at php end. $_REQUEST would work for $_GET/$_POST requests.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search