Как отправить JavaScript-переменную exampleId в php-файл https://example.com/widget/widget-updater.php из функции $ .ajax (), приведенной ниже?
jQuery(document).ready(function($) {
var element = document.getElementById('widget');
var exampleId = element.getAttribute('attributeExample');
alert(exampleId); /* This prints out correctly into a pop up box */
$.ajax({
type: "GET",
url: "https://example.com/widget/widget-updater.php"
});
})();
Я знаю, как получить переменную, если еев PHP с $ _GET ['exampleId'], но я не знаю, как отправить его в файл PHP с помощью функции $ .ajax () выше.Благодарю.
Вот код HTML:
<script
src="https://example.com/widget/externalJSfileContainingTheCodeBelow.js"
async></script><div id="widget" dataVar="1"></div>
Вот код Javascript / jQuery
(function () {
var jQuery;
if (window.jQuery === undefined) {
var script_tag = document.createElement('script');
script_tag.setAttribute("type", "text/javascript");
script_tag.setAttribute("src",
"https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js");
if (script_tag.readyState) {
script_tag.onreadystatechange = function () {
if (this.readyState == 'complete' || this.readyState == 'loaded') {
scriptLoadHandler();
}
};
} else {
script_tag.onload = scriptLoadHandler;
}
(document.getElementsByTagName("head")[0] ||
document.documentElement).appendChild(script_tag);
} else {
jQuery = window.jQuery;
main();
}
function scriptLoadHandler() {
jQuery = window.jQuery.noConflict(true);
main();
}
function main() {
jQuery(document).ready(function ($) {
var element = document.getElementById('widget');
var numberVariable = element.getAttribute('dataVar');
alert(numberVariable);
$.ajax({
type: "GET",
url: "https://example.com/widget/widget-updater.php",
data: { id: numberVariable }
});
var css_link = $("<link>", {
rel: "stylesheet",
type: "text/css",
href: "https://example.com/widget/widget.css"
});
css_link.appendTo('head');
var jsonp_url = "https://example.com/cgi-bin/data.py?callback=?";
$.getJSON(jsonp_url, function (data) {
$('#widget').html(data.html);
});
});
}
})();