0

How may I take the input from a textbox in HTML, using autocomplete, in order to feed that data into my url parameter via ajax? My goal is to output the data into HTML. The type of data that I am querying is an XML API.

This is my html:

<input id="data_from_autocomplete"> 
<button type="submit>Submit</button>

This is my jQuery:

$.ajax({
type: "GET",
url: "http://www.something" + data_from_autocomplete + ".com",
dataType: "xml",
success: parse
});    
6
  • 2
    url: "http:www.something" + $('#data_from_autocomplete').val() + ".com", but I doesn't follow your idea to be honest. Commented Jan 14, 2017 at 17:05
  • Is somehow unclear what you want to achieve, can you provide a sample on the actual values that the url can take? Commented Jan 14, 2017 at 17:06
  • 1
    This sounds like an XY problem Commented Jan 14, 2017 at 17:12
  • My goal is to take input from a text box and add it inside a API call in AJAX. This is because I would like to ask the user for some input, feed that data to the AJAX API call, and then output the results on the web page. Commented Jan 14, 2017 at 17:45
  • so send it as data, no need to manipulate the url yourself Commented Jan 14, 2017 at 17:53

1 Answer 1

3

Use

var param = $("#data_from_autocomplete").val();
var url =  "http://www.something" + encodeURIComponent(param) + ".com";

//call your ajax

[update]

If you need to pass the value of the search field as parameter, just pass it in the data parameter of the ajax call:

var city = $("#data_from_autocomplete").val();
var state = "wa";

$.ajax({
    url : "https://www.zillow.com/webservice/GetRegionChildren.htm",
  data : {
    "zws-id": /*your zws-id goes here*/,
    state : state,
    city: city
  },
  success: function(response) {
    //process your response here
  }
});
Sign up to request clarification or add additional context in comments.

7 Comments

Thank you kindly for the suggestion, but this does not work.
Please clarify your question.
My goal is to take input from a text box and add it inside a API call in AJAX. This is because I would like to ask the user for some input, feed that data to the AJAX API call, and then output the results on the web page.
I don't understand your question, the first answer i provided did what you initially requested, then you say is not working. What url are you trying to call, i doubt that the api you try to call listen in different urls, let alone based on user input. You are probably trying to call www.someserver.com?search=[query] or www.someserver.com/search/[query]
Please provide examples of the urls that can be called
|

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.