0

I am able to pass plain text to my api's url as follows:

var bus = $('.location').text(); $.getJSON('http://apiofsite'+bus+'apiofsitecontinued.com'), function(){ do something});

Now I want to be able to do the same thing, but using the input field instead of printed HTML. Something like so:

<input id="stuff" type="input" />

So ideally my JS would be like so.

var sang = $('#stuff').val(); $.getJSON('http://apiofsomewebsite'+sang+'apiofsomewebsitecontinued.com'), function(){ do something});

I don't believe that the URL is reading my input, as it returns empty.

4
  • 1
    Is this code within a function that runs onClick, after the user has entered a term? Commented May 2, 2016 at 22:45
  • it's run by $(document).on('click', '#somerandomdiv', function(){myproblemhere}); after user has entered into #stuff Commented May 2, 2016 at 22:50
  • Can you provide a working example through SO's embedded code, or a jsFiddle? Commented May 2, 2016 at 22:54
  • Figured it out. Since I can't delete this question I'll just share my answer here. Basically, when transferring input values into an API url, be sure to format your string. So, for instance, in my example, you should add var sang = $('#stuff').val().replace(/\s+/g, '') to remove spaces and extra characters that should not be in the url. Commented May 2, 2016 at 23:40

1 Answer 1

1

This doesn't seem to have anything to do with the actual ajax call. Try this:

var url = 'http://apiofsomewebsite'+ $('#stuff').val() + 'apiofsomewebsitecontinued.com';
console.log(url);
//$.getJSON(url, function() {...});

If the url is not the one you expected, check to see you are placing the code in the right event (i.e. the field's change event, a button's click event, or the form's submission etc.) when the input value is available.

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

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.