Magazine
 
Quick Review:Ajax
 

Automate data entry with Web services and Ajax

 

@address = Address.find(params[:id])
if @address.update_attributes(params[:address])
flash[:notice] = ‘Address was successfully updated.’
end
end
# Build the XML to call the Web service
xm = Builder::XmlMarkup.new
xmlstuff = xm.CityStateLookupRequest(“USERID”=>”XXXXXXXXXXXX”) {
xm.ZipCode(“ID”=>”0") {
xm.Zip5(params[:zip5]) }}
webservice = ‘http://testing.shippingapis.com/ShippingAPITest.dll?’
uri_enc = URI.escape(webservice + ‘API=CityStateLookup&XML=’ + xmlstuff)
uri = URI.parse(uri_enc)
# The call to the Web service — response is in var ‘doc’
doc = REXML::Document.new open(uri)
logger.debug(“doc = “ + doc.to_s)
doc.elements.each(“CityStateLookupResponse/ZipCode”) { |element|
#logger.debug(element.attributes[“name”])
logger.debug(element)
logger.debug(“element[0] = “ + element[0].to_s)
logger.debug(“element[0].text = “ + element[0].text)
logger.debug(“element[1] = “ + element[1].to_s)
logger.debug(“element[1].text = “ + element[1].text)
logger.debug(“element[2] = “ + element[2].to_s)
logger.debug(“element[2].text = “ + element[2].text)
# Set the model field values to the response from the web service
@address.city = element[1].text
@address.state = element[2].text
}
end # valid ZIP code if-statement-checkers
render :partial => “cityStateFields”
end

June   2008 | Java Jazz Up |23
 
previous
index
next
 
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,

30
, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,   Download PDF