我是AJAX世界的新手,所以请原谅我的n00bishness。我有一个XML文档,如下所示:
<juices>
<smoothie id="SY4301">
<name>Strawberry Yumghurt</name>
<price currency="GBP">
<perunit type="wholesale">1.23</perunit>
<perunit type="RRP">1.89</perunit>
<percrate>28.50</percrate>
</price>
<description>Velvety yoghurt smoothie made with fresh strawberry and rasperries.</description>
<ingredients>
<ingredient image="/images/strawberry.jpg" quantity="9">Strawberries</ingredient>
<ingredient image="/images/Raspberry.jpg" quantity="4">Raspberries</ingredient>
<ingredient image="/images/yoghurt.jpg"quantity="200ml">Greek Yoghurt</ingredient>
<ingredient image="/images/honey.jpg" quantity="30ml">Runny Honey</ingredient>
<ingredient image="/images/milk.jpg"quantity="40ml">Semi-skimmed Milk</ingredient>
</ingredients>
<nutrition per="100ml">
<kcal unit="cal">140</kcal>
<kjoules unit="KJ">442</kjoules>
<carbohydrate unit="g">27.9</carbohydrate>
<protein unit="g">1.4</protein>
<fat unit="g">8.4</fat>
<fibre unit="g">2.8</fibre>
</nutrition>
<imageloc>http://www5.bbk.ac.uk/~akaufm01/jvfma/Images/Products/morning_kick.jpg</imageloc>
</smoothie>
</juices>
我有一个XSL文档正在加载除页面加载之外的所有内容,但我想要实现的是点击(链接或按钮,我不介意哪个),替换其中一个div的内容(#description),其中包含其图像的成分列表,其位置存储在图像属性中。
我已经看了一些教程,建议使用jQuery来实现这一点,但我没有太多运气让它工作。我知道我也可以在JS中使用XMLHttpRequest函数,但不确定哪种方法更好。
非常感谢任何建议!
谢谢, 安迪
答案 0 :(得分:0)
首先你需要了解你在使用ajax做什么,因为有不同的方法来使用jquery调用ajax,让我们使用基本属性
//Handle the ajax in the event click of your button with ID MyButton
$('#MyButton').click(function(){
$.ajax({
type: "Type of your http Request could be GEt or Post",
url: " the url where you obtain that xml",
data: "ïf you need to pass some data to the url"
dataType: "in your case should be XML which is the data type that you're going to handle in the success function",
success: function(data){
//parse your xml which it contains in the var data
//You could set the data parse in your div with a append or html method
}'define a function or put a function already defined',
error: 'define a function in case of error'
});
});
Here's the doc about an ajax in jquery