Javascript Wordpress AJAX HTML5地理位置(错误400)
我在Wordpress中使用AJAX获取HTML5地理位置并使用Javascript Wordpress AJAX HTML5地理位置(错误400),javascript,jquery,ajax,wordpress,Javascript,Jquery,Ajax,Wordpress,我在Wordpress中使用AJAX获取HTML5地理位置并使用ajaxurl(admin AJAX.php)输出到Google地图嵌入时遇到问题 在包含ajaxurl(admin ajax.php)之前,这是有效的(单击Allow Location),并显示了带有经度和纬度的嵌入式谷歌地图),但如果不使用ajaxurl(admin ajax.php),我无法将经度和纬度变量传递给php。我需要PHP中的变量,以便在其他函数/进程中使用它们 已更新 我已经用@Parthavi Patel的建议更
ajaxurl(admin AJAX.php)
输出到Google地图嵌入时遇到问题
在包含ajaxurl(admin ajax.php)
之前,这是有效的(单击Allow Location
),并显示了带有经度和纬度的嵌入式谷歌地图),但如果不使用ajaxurl(admin ajax.php)
,我无法将经度和纬度变量传递给php。我需要PHP中的变量,以便在其他函数/进程中使用它们
已更新
我已经用@Parthavi Patel的建议更新了下面的代码。我没有链接add_action(…,'audp_locate_book_request')代码>包括$.ajax数据{操作:'audp\u locate\u book\u request',…}
。下面的代码示例已更新为正确且可操作
CUSTOM-PLUGIN.PHP
public function audp_locate_book() {
$post = get_post();
if ( is_single('book') || ( is_single() && 'book' == $post->post_parent ) ) {
wp_enqueue_script (
'audp_locate_book',
plugin_dir_url( __FILE__ ) . 'js/audp-locate-book.js',
array('jquery'),
$this->version, // (Plugin uses Bootstrap)
true
);
wp_localize_script(
'audp_locate_book',
'audp_locate_book_obj',
array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) )
);
}
}
public function audp_locate_book_request() {
if ( isset ( $_REQUEST ) ) {
print_r( $_REQUEST );
$longitude = $_REQUEST['longitude'];
$latitude = $_REQUEST['latitude'];
}
wp_die();
}
/**
* Plugin uses bootstrap template. Alternatively you just use:
* add_action( 'wp_enequeue_scripts, 'audp_locate_book' );
* & repeat for other functions.
*/
$this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'audp_locate_book' );
$this->loader->add_action( 'wp_ajax_audp_locate_book_request', $plugin_public, 'audp_locate_book_request'); // Logged In
$this->loader->add_action( 'wp_ajax_nopriv_audp_locate_bookt_request', $plugin_public, 'audp_locate_book_request'); // Not Logged In
<!-- Relevant Code -->
<a onClick="javascript:void(0);" id="geolocate" class="button primary expand" >Allow Location</a>
<p style="text-align: justify; font-size: 9.5pt;" id="map"></p> <!-- Map No Longer Shows Here -->
<!-- End Relevant Code -->
AUDP-LOCATE-BOOK.JS
(function($){
$(function() {
jQuery('a#geolocate').click(initiate_geolocation);
// Initiate Geolocation
function initiate_geolocation() {
navigator.geolocation.getCurrentPosition(geolocation_query,geolocation_errors);
}
// Output Geolocation
function geolocation_query(position){
var longitude = position.coords.longitude;
var latitude = position.coords.latitude;
$.ajax({
type: "POST",
url: audp_locate_book_obj.ajaxurl,
data: { action: 'audp_locate_book_request', longitude: position.coords.longitude, latitude: position.coords.latitude },
success: function(data) {
jQuery("a#geolocate").html("Location Recorded").attr("disabled", true);
jQuery("p#map").html('<iframe src="https://maps.google.com/maps?q=' + latitude + ',' + longitude + '&z=17&output=embed" width="100%" height="170" frameborder="0" style="border:0"></iframe>');
console.log("Log: " + longitude + " & " + latitude);
}
});
}
// Geolocation Errors
function geolocation_errors(error) {
switch(error.code) {
case error.PERMISSION_DENIED: alert("Location denied by user.");
break;
case error.POSITION_UNAVAILABLE: alert("Location could not be detected.");
break;
case error.TIMEOUT: alert("Location request timed out.");
break;
default: alert("Unknown error.");
break;
}
}
});
})(jQuery);
第23行是指:
23: $.ajax({
24: type: "POST",
25: url: audp_locate_book.ajaxurl,
... etc
SINGLE-POST-TYPE.PHP
public function audp_locate_book() {
$post = get_post();
if ( is_single('book') || ( is_single() && 'book' == $post->post_parent ) ) {
wp_enqueue_script (
'audp_locate_book',
plugin_dir_url( __FILE__ ) . 'js/audp-locate-book.js',
array('jquery'),
$this->version, // (Plugin uses Bootstrap)
true
);
wp_localize_script(
'audp_locate_book',
'audp_locate_book_obj',
array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) )
);
}
}
public function audp_locate_book_request() {
if ( isset ( $_REQUEST ) ) {
print_r( $_REQUEST );
$longitude = $_REQUEST['longitude'];
$latitude = $_REQUEST['latitude'];
}
wp_die();
}
/**
* Plugin uses bootstrap template. Alternatively you just use:
* add_action( 'wp_enequeue_scripts, 'audp_locate_book' );
* & repeat for other functions.
*/
$this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'audp_locate_book' );
$this->loader->add_action( 'wp_ajax_audp_locate_book_request', $plugin_public, 'audp_locate_book_request'); // Logged In
$this->loader->add_action( 'wp_ajax_nopriv_audp_locate_bookt_request', $plugin_public, 'audp_locate_book_request'); // Not Logged In
<!-- Relevant Code -->
<a onClick="javascript:void(0);" id="geolocate" class="button primary expand" >Allow Location</a>
<p style="text-align: justify; font-size: 9.5pt;" id="map"></p> <!-- Map No Longer Shows Here -->
<!-- End Relevant Code -->
允许位置
最终,我的目标是显示嵌入经度/纬度的google地图,并使经度和纬度成为一个PHP变量($longitude
,$latitude
),因为我需要它用于页面中的其他PHP函数 同时在数据操作中传递操作名称:“audp\u locate\u book\u request”
$.ajax({
type: "POST",
url: audp_locate_book.ajaxurl,
data: { action: 'audp_locate_book_request', longitude: position.coords.longitude, latitude: position.coords.latitude },
success: function(data) {
jQuery("a#geolocate").html("Location Recorded").attr("disabled", true);
jQuery("p#map").html('<iframe src="https://maps.google.com/maps?q=' + latitude + ',' + longitude + '&z=17&output=embed" width="100%" height="170" frameborder="0" style="border:0"></iframe>');
console.log("Log: " + longitude + " & " + latitude);
}
});
$.ajax({
类型:“POST”,
url:audp_locate_book.ajaxurl,
数据:{操作:'audp_locate_book_request',经度:position.coords.longitude,纬度:position.coords.latitude},
成功:功能(数据){
jQuery(“a#geologite”).html(“记录的位置”).attr(“禁用”,true);
jQuery(“p#map”).html(“”);
日志(“日志:“+经度+”&“+纬度”);
}
});
谢谢你,帕塔维。除了函数audp_locate_book_request(),我如何将该函数中的变量(或任何相关信息)输出回页面?我尝试了var_dump($请求)和var_dump($经度)等,但没有结果?您可以在“网络”选项卡中检查post
数据。只要做echo';打印(邮政美元);死亡代码>在此之前,请确保在这两个变量中获得正确的值<代码>变量经度=position.coords.longitude;变量纬度=位置坐标纬度代码>