English 中文(简体)
Remove javascript in response ajax
原标题:Remove javascript in response ajax

现在,这个问题实际上并不了解为什么,这是我第一次处理这个问题。

www.un.org/spanish/ga/president

  1. i code php wordpress.
  2. i have ajax request.
  3. 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."}}
问题回答

I suggest that you move the JavaScript code from ajax.php to a separate file. Then use WordPress s wp_enqueue_script() function to properly enqueue this JavaScript file. eg.

function enqueue_custom_scripts() {
    wp_enqueue_script( ajax-script , plugin_dir_url(__FILE__) .  ajax-script.js , array( jquery ), null, true);

    // Pass AJAX URL to JavaScript
    wp_localize_script( ajax-script ,  ajax_object , array( ajax_url  => admin_url( admin-ajax.php )));
}
add_action( wp_enqueue_scripts ,  enqueue_custom_scripts );

你的检验标准是:ajax。 php应保持不变。





相关问题
ajax login using httpRequest?

I am trying to develop my login script to give feedback to the user if the login is valid or not. Basically if it isn t correct a div box will show saying its wrong, if its correct it will show its ...

Virtual Tour using sketch up, ajax, flash technologies

I want to know if there are existing technology that make your 3d models in sketch into virtual tours, using either Ajax or Flash for web presentation. If there s none, which will be a good approach ...

How can i update div continuously

I have asp.net application where i have a div which showing the value from other site. The value of that site is changing continuously. I want that my div will automatically update in some interval ...

热门标签