Search code examples
phphtmljsongetlocalhost

$.getJSON don't work in the second link page of my local site


I have a problem with the $.getJSON function. The get works fine on my first page - index.html. However, when I go to my second page - second.html and use the function $.getJSON I get an error.

The code (second.html) does not work:

$(document).ready(function(){
   $('button').click(search);
});

function search(){
    var name = $('#inputName').val();
    $.getJSON("api/research_name.php?nome="+nome, function(data){
        console.log(data);
    });
}

This code (index.html) works fine:

$(document).ready(function(){
   $('button').click(search);
});
function search(){
    var name = $('#inputName').val();
    $.getJSON("api/research_name.php?nome="+nome, function(data){
        console.log(data);
    });
}

Solution

  • (this would probably be better as a comment, but)

    check your code. you assign name to the value of an input field, but in your .getJSON call use nome

    Also, you should probably use urls that are fully qualified. For example, if your hierarchy is

    /index.html

    /folder/something/second.html

    then your api call will be relative to the directory in which it was called, which might not be the root.

    Consider changing it to "/api/research_name.php?nome="+nome