现在,这个问题实际上并不了解为什么,这是我第一次处理这个问题。
www.un.org/spanish/ga/president
- i code php wordpress.
- i have ajax request.
- the response of this ajax request will be shown object. but my response include javascript
这里是亚.。 php file:
<?php
function test_ajax()
{
$data = [
success => true,
message => has been load successfully. ,
];
wp_send_json_success($data);
}
add_action( wp_ajax_test_ajax , test_ajax );
add_action( wp_ajax_nopriv_test_ajax , test_ajax );
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function () {
var indexingMessage = $("#indexing-message");
$("#index-urls").click(function (event) {
event.preventDefault(); // Prevent default form submission
console.log("Loading...");
indexingMessage.text("Loading...");
var urls = $("#urls").val().trim().split(/
?
/); // Split URLs by newlines
if (!urls.length) {
indexingMessage.text("Please enter URLs.");
return;
}
function indexUrl(url) {
$.ajax({
url: "<?php echo admin_url( admin-ajax.php ); ?>",
type: "POST",
data: {
action: "test_ajax",
url: url,
},
dataType: json ,
success: (response_ajax) => {
console.log("Response AJAX:", response_ajax);
if (response_ajax.success) {
indexingMessage.text("Success:: " + url);
} else {
indexingMessage.text("Error! An error occurred. Please try again later. Please check your browser s log for details");
}
},
error: (xhr, status, error) => {
console.log("Error AJAX:", JSON.stringify(error));
indexingMessage.text("Error! An error occurred. Please try again later. Please check your browser s log for details.");
}
});
}
for (let i = 0; i < urls.length; i++) {
indexUrl(urls[i]);
}
});
});
</script>
这里是装载机。 php
<?php
/*
Plugin Name: Load URLs
Plugin URI:
Description: Custom author template for WordPress.
Version: 1.0
Author URI:
License: GPL2
*/
include_once ( ajax.php );
add_action( admin_menu , my_plugin_settings_page );
function my_plugin_settings_page()
{
add_menu_page(
My Plugin Settings ,
My Plugin ,
manage_options ,
my-plugin-settings ,
my_plugin_settings_content ,
dashicons-admin-settings // Optional icon
);
}
function my_plugin_settings_content()
{
?>
<div class="wrap">
<h1>My Plugin Settings</h1>
<form method="post" action="options.php">
<?php settings_fields( my-plugin-settings-group ); ?>
<div id="my-plugin-settings-section">
<h3>key</h3>
<table class="form-table">
<tr valign="top">
<th scope="row"><label for="key"> Key</label></th>
<td>
<textarea style="height:200px;width:100%" type="text" name="key" id="key"
size="60" ><?php echo esc_attr(get_option( key )); ?></textarea>
<p class="description">Enter your key here.</p>
</td>
</tr>
</table>
</div>
<?php submit_button( Save Changes ); ?>
</form>
<h3> URLs</h3>
<table>
<tr valign="top">
<th scope="row"><label for="urls">URLs (separate URLs with newlines)</label></th>
<td>
<textarea style="height:200px;width:100%" name="urls" id="urls" rows="5"
cols="50"></textarea>
<p class="description">Enter each URL on a separate line.</p>
</td>
</tr>
</table>
<p>Click the button below to load the URLs you entered above.</p>
<button id="index-urls" class="button button-primary">Load URLs</button>
<p id="indexing-message"></p>
</div>
<?php
}
add_action( admin_init , my_plugin_register_settings );
function my_plugin_register_settings()
{
register_setting( my-plugin-settings-group , key , sanitize_text_field );
}
?>
结果是:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script>
$(document).ready(function () {
var indexingMessage = $("#indexing-message");
$("#index-urls").click(function (event) {
event.preventDefault(); // Prevent default form submission
console.log("Loading...");
indexingMessage.text("Loading...");
var urls = $("#urls").val().trim().split(/
?
/); // Split URLs by newlines
if (!urls.length) {
indexingMessage.text("Please enter URLs.");
return;
}
function indexUrl(url) {
$.ajax({
url: "https://192.168.146.1/kiguiadsence/wp-admin/admin-ajax.php",
type: "POST",
data: {
action: "test_ajax",
url: url,
},
dataType: json ,
success: (response_ajax) => {
console.log("Response AJAX:", response_ajax);
if (response_ajax.success) {
indexingMessage.text("Success:: " + url);
} else {
indexingMessage.text("Error! An error occurred. Please try again later. Please check your browser s log for details");
}
},
error: (xhr, status, error) => {
console.log("Error AJAX:", JSON.stringify(error));
indexingMessage.text("Error! An error occurred. Please try again later. Please check your browser s log for details.");
}
});
}
for (let i = 0; i < urls.length; i++) {
indexUrl(urls[i]);
}
});
}); </script>{"success":true,"data":{"success":true,"message":" has been load successfully."}}
hope everyone can help me. I just want the return result to be
{"success":true,"data":{"message":" has been load successfully."}}