yoda
yoda

Reputation: 10981

jQuery ajax not loading

I have an input with the purpose of being a part of a search engine, on wich I use jQuery to pre-filter the search string removing forbidden characters as long as other things. Recently I had to change the url of the website and used PHP to print a variable that would indicate the url for ajax requests (as I was doing before, except that now normal and ajax requests have different url's), and I found a problem that I can't figure out what it is.

Basically the ajax request doesn't work (no actions and no request at all on Firebug). Tried every possible way (declaration inside the funcion, passed as argument, and so on) to tell my script the url I wanted, but it only works with the old url (even the ajax request never gets called).

So I'd like you to check it and see if you find anything wrong.

Here's the code :

$(function() {

    $('form#search-form').submit(function(e) { search(e, acc); });
    $('a#search-submit').click(function(e) { search(e, acc); });

});

function search(e, l)
{
    e.preventDefault();

    var t = $('#search-text input[name="search-text"]').val();
    //var l = $('#nav-ul li.sel a').attr('href');

    $.ajax({
        type: 'POST',
        url: l+'format_search_string',
        data: 's='+t,
        cache: false,
        dataType: 'json',
        success: function(response)
        {
            if (response.status == 'true')
                window.location = $('#search-submit').attr('href')+'/s:'+response.string;
            else
                jQuery.facebox('<p class="facebox-notice">Necessita preencher o campo da pesquisa</p>');
        }
    });
}

edit: "acc" is a variable containing the url, wich prints exacly what I want when I call it inside search() function, the only thing that does nothing is the ajax request (isn't called at all). If I use the old url, it works, but the strange thing is that with the new url, at least the ajax call should be made.

The code is all in the same server and domain, and nothing as changed, just the url of the requests.

normal request :

http://category.domain.com/

ajax request :

http://www.domain.com/category

Upvotes: 2

Views: 321

Answers (2)

ehudokai
ehudokai

Reputation: 1926

If you moved your backend to a different server. try changing your request to being a jsonp type, and not just json.

Upvotes: 0

Gabriele Petrioli
Gabriele Petrioli

Reputation: 196236

Sound like you are requesting from a different domain. And this is not allowed.

Look up same-origin-policy

Only JSONP allows for cross-site requests.

Also have a look at another SO question/answer: Same Origin Policy - AJAX & using Public APIs

Upvotes: 2

Related Questions