minx
minx

Reputation: 27

Building a URL in AJAX via an HTML input

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
});    

Upvotes: 0

Views: 57

Answers (1)

james_bond
james_bond

Reputation: 6906

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
  }
});

Upvotes: 3

Related Questions