2

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
1
  • Is the code above hosted on the same server, just the backend code moved to a different server? Commented Jan 4, 2011 at 22:25

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, wasn't aware that I could't do requests from one subdomain to another. Cheers!
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.