Sunday, January 8, 2023
HomeWordPress Development(I've tried and tried) Unhealthy Request 400... jQuery ajax put up to...

(I’ve tried and tried) Unhealthy Request 400… jQuery ajax put up to wordpress registered operate


I’ve tried and tried and tried, I’ve searched and searched… I can not determine why this is not working. One large downside is I can not appear to determine the best way to get any good debug information about why that is failing.

Anyway, I’ve a search area to lookup libraries from CDNJS.com (which works). Upon number of a consequence, I’m wanting so as to add that library information to a customized WordPress desk. That course of is failing, it doesn’t matter what I strive.

This is the code:

<?php

    operate cdnjs_add_library_callback() {
        // Get the library information from the POST request
        $library = json_decode(file_get_contents('php://enter'), true);

        // Insert the library into the "cdnjs_libraries" desk
        international $wpdb;
        $table_name = $wpdb->prefix . 'cdnjs_libraries';
        $wpdb->insert(
            $table_name,
            array(
                'description' => $library['description'],
                'homepage' => $library['homepage'],
                'newest' => $library['latest'],
                'license' => $library['license'],
                'identify' => $library['name'],
                'model' => $library['version']
            )
        );

        // Verify for errors
        if ($wpdb->last_error) {
            die("Error inserting library: " . $wpdb->last_error);
        }

        // Return successful message
        echo "Library added efficiently";

        wp_die(); // that is required to terminate instantly and return a correct response
    }
    add_action( 'wp_ajax_cdnjs_add_library', 'cdnjs_add_library_callback' );

?>

    <div class="wrap">
        <h1>CDNJS Library Supervisor</h1>
        <p>&nbsp;</p>
        <div id="search-form">
            <type onsubmit="return false;">
                <h2><label for="library-search">Search CDNJS</label></h2>
                <enter sort="textual content" id="library-search" identify="library-search" placeholder="Kind to begin looking..." measurement=30>
            </type>
        </div>
        <p>&nbsp;</p>
        <h2>Managed Libraries</h2>
        <desk id="managed-libraries" class="wp-list-table widefat fastened striped">
            <thead>
                <tr>
                    <th>Motion</th>
                    <th>Filename</th>
                    <th>Description</th>
                    <th>Model</th>
                    <th>License</th>
                    <th>Homepage</th>
                    <th>Delete</th>
                </tr>
            </thead>
            <tbody>
                <?php echo generate_table_rows($managed_libraries); ?>
            </tbody>
        </desk>
    </div>

    <script>
    jQuery(doc).prepared(operate($) {

        // Arrange the search area as an autocomplete area
        $("#library-search").autocomplete({
            // Set the supply of the autocomplete choices to the CDNJS API
            supply: operate(request, response) {
                $.ajax({
                    information: {
                        search: request.time period,
                        fields: "identify,description,model,license,homepage"
                    },
                    success: operate(outcomes) {
                        console.log(outcomes);
                        // Extract the record of library names, descriptions, and URLs from 
                        // the outcomes and go them to the response callback
                        var libraries = outcomes.outcomes.map(operate(library) {
                            return {
                                label: "<b>"+library.identify+"</b><br><i>"+library.description+"</i>",
                                worth: library
                            };
                        });
                        response(libraries);
                    },
                    url: "https://api.cdnjs.com/libraries"
                });
            },
            minLength: 3,
            choose: operate(occasion, ui) {
                // When a library is chosen from the autocomplete record,
                // add that library to the "cdnjs_libraries" desk by 
                // making an AJAX POST name to the "cdnjs_add_library" PHP motion

                // Get the chosen library object
                var library = ui.merchandise.worth;

                // Make an AJAX name to the "cdnjs_add_library" motion
                $.ajax({
                    url: ajaxurl,
                    sort: 'POST',
                    dataType: 'json',
                    contentType: 'utility/json; charset=utf-8',
                    information: JSON.stringify({
                        motion: 'cdnjs_add_library',
                        library: library
                    }),
                    success: operate(response) {
                        console.log(response);
                        // Add the library to the desk of managed libraries
                        $("#managed-libraries tbody").append(generateTableRow(library));
                    },
                    error: operate(error) {
                        console.log(error);
                    }
                });
            }
        }).autocomplete( "occasion" )._renderItem = operate( ul, library ) {
            // Customise the show of the autocomplete choices
            // Create a brand new record merchandise for the library
            var listItem = $("<li>");
            // Create a hyperlink for the library
            var libraryLink = $("<a>", {
                href: library.url,
                goal: "_blank",
                html: library.label
            });
            // Append the hyperlink to the record merchandise
            listItem.append(libraryLink);
            // Return the record merchandise
            return listItem.appendTo(ul);
        };


    });
    </script>

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments