试试这个http://tinker.io/37211
项目 HTML
<table border="1" cellpadding="5">
<tr>
<td> Product 1 </td>
<td>
<button type="button" class="SaveItemButton"
data-item-name="Product 1" data-item-price="1" data-item-description="Description 1" data-retailer-item-url="URL 1">
Save Item
</button>
</td>
</tr>
<tr>
<td> Product 2 </td>
<td>
<button type="button" class="SaveItemButton"
data-item-name="Product 2" data-item-price="2" data-item-description="Description 2" data-retailer-item-url="URL 2" />
Save Item
</button>
</td>
</tr>
</table>
包括 JavaScript
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script>
(function($){
window.SomeApiKey = '';
// This is the JS file contents which will be saved as file somewhere and included like the jQuery library above
$(document).ready(function(){
$('.SaveItemButton').click(function(){
var btn = $(this);
// colect data
var data = {'item-name': btn.data('item-name'),
'item-price': btn.data('item-price'),
'item-description': btn.data('item-description'),
'retailer-item-url': btn.data('retailer-item-url'),
'api-key': window.SomeApiKey};
// this line just for testing
alert(' will send :' + $.param( data ) );
// do JSONP request
$.ajax({
type: "GET",
url: "http://www.site.com",
dataType: "jsonp",
data: data
}).done(function( msg ) {
alert( msg );
});
return false;
});
});
})(jQuery);
</script>
<script>
// here the client will set APIkey to use
window.SomeApiKey = 'APIKEY';
</script>