1
function change_tag(url){
  $('.filter_button_confirm').unbind('click').on('click', function(){
    ...
    console.log(url);

    $.ajax({
      url: url,
      type: 'POST',
      data: {
        ...
      },
      beforeSend: function(){
        ...
      }
    })
      .done(function(data){
        ...
      })
  });
};

When I call the function with argument change_tag('/some_url/') of course it works.

But, change_tag() also works perfectly.

It brings data from the url that I set in the past.

console.log(url) shows 'undefined' so I don't know how it knows the url.

4
  • What do you want the solution to be? Should the code throw an error if no url is given? Commented Dec 21, 2016 at 14:45
  • 1
    What if you rename your parameter 'url', to check if you are not using a global variable ? Commented Dec 21, 2016 at 14:46
  • 1
    Did you declare url globally? Commented Dec 21, 2016 at 14:46
  • I want to know how it works. I'm afraid error occurs later. Commented Dec 21, 2016 at 14:48

1 Answer 1

9

Since you're not passing in a parameter to the function, url is undefined and so the ajax call gets called with url: undefined. This is the same as not providing an url, and makes it post to the url you're currently on.

For more info you can check the jQuery page: http://api.jquery.com/jquery.ajax/

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

2 Comments

Calling change_tag() actually calls change_tag with url as undefined, not null.
But the OP says that the console log shows undefined for the URL and adds that "It brings data from the url that I set in the past.", not the current URL

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.