简体   繁体   English

从ajax切换时未调用jquery

[英]jquery not being called when switched from ajax

I had an ajax function, that needed to be converted to being called with Jquery for wordpress - but now the function doesn't get called? 我有一个ajax函数,需要将其转换为用Jquery进行wordpress调用-但是现在不调用该函数吗?

    function getCategories()
        {

    alert('getCategories test');
            var fData = new Object();
            fData.val = '';

            jQuery(document).ready(
            {
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "php_scripts/getdeals_php.php",
                data: '{"action":"GetCats", "fData":' + JSON.stringify(fData) + '}',
                dataType: "json",
                success: function (msg)
                {
                    alert('Success');
                    var offerList = msg;
                    var Cats = document.getElementById('CategoriesSelect');
                    document.getElementById("CategoriesSelect").options.length = 0;
                    var optn    = document.createElement('option');
                        optn.text   = "Select Category";
                        optn.value  = "Select Category";        
                        Cats.add(optn);


                    for(var i=0;i<offerList.length;i++)
                    {
                        var optn    = document.createElement('option');
                        optn.text   = offerList[i];
                        optn.value  = offerList[i];     
                        Cats.add(optn);
                    }
                },
                error: function (xhr, ajaxOptions, thrownError)
                {
                    alert("ERROR:" + xhr.responseText+" - "+thrownError);
                }
            });

        }

I get my 'getCategories test' alert but I don't get the 'success' or the 'error' alert 我收到“ getCategories test”警报,但没有收到“成功”或“错误”警报

So I do not think the jquery is running. 所以我不认为jQuery正在运行。

Before I started integrating this with wordpress I was using ajax like this 在开始将其与wordpress集成之前,我正在使用像这样的ajax

$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: "php_scripts/getdeals_php.php",
    data: '{"action":"GetCats", "fData":' + JSON.stringify(fData) + '}',
    dataType: "json",
    success: function (msg)
    {
        $('#loadingmessage').hide();
        var offerList = msg;
        var Cats = document.getElementById('CategoriesSelect');
        document.getElementById("CategoriesSelect").options.length = 0;
        var optn    = document.createElement('option');
            optn.text   = "Select Category";
            optn.value  = "Select Category";        
            Cats.add(optn);


        for(var i=0;i<offerList.length;i++)
        {
            var optn    = document.createElement('option');
            optn.text   = offerList[i];
            optn.value  = offerList[i];     
            Cats.add(optn);
        }
    },
    error: function (xhr, ajaxOptions, thrownError)
    {
        alert("ERROR:" + xhr.responseText+" - "+thrownError);
    }
});

I would get an erorr that ajax was unknown, and it appears I should be using jquery for this instead...? 我会得到一个未知的ajax的错误信息,看来我应该为此使用jquery ...?

I get no errors in the console 我在控制台中没有任何错误

Well, there are some different method for calling Ajax in WP. 嗯,在WP中有一些不同的方法来调用Ajax。 For instance, one way would be like 例如,一种方式是

FOR JS Piece FOR JS件

jQuery(document).ready(function($) {

    // We'll pass this variable to the PHP function example_ajax_request
    var fruit = 'Banana';

    // This does the ajax request
    $.ajax({
        url: ajaxurl,
        data: {
            'action':'example_ajax_request',
            'fruit' : fruit
        },
        success:function(data) {
            // This outputs the result of the ajax request
            console.log(data);
        },
        error: function(errorThrown){
            console.log(errorThrown);
        }
    });   

});

and PHP piece 和PHP片

function example_ajax_request() {

        // The $_REQUEST contains all the data sent via ajax 
        if ( isset($_REQUEST) ) {

            $fruit = $_REQUEST['fruit'];

            // Let's take the data that was sent and do something with it
            if ( $fruit == 'Banana' ) {
                $fruit = 'Apple';
            }

            // Now we'll return it to the javascript function
            // Anything outputted will be returned in the response
            echo $fruit;

            // If you're debugging, it might be useful to see what was sent in the $_REQUEST
            // print_r($_REQUEST);

        }

        // Always die in functions echoing ajax content
       die();
    }

    add_action( 'wp_ajax_example_ajax_request', 'example_ajax_request' );

    // If you wanted to also use the function for non-logged in users (in a theme for example)
    // add_action( 'wp_ajax_nopriv_example_ajax_request', 'example_ajax_request' );

Thanks to wptheming 多亏了wptheming

In your case, first try to follow template for JS part and secondly always remember in WP to prevent conflict, you must always use 对于您的情况,首先尝试遵循JS部分的模板,其次要始终记住在WP中以防止冲突,您必须始终使用

jQuery(document).ready(function($) {
     // You JS functions 
});

So I tried to modify your code now on the fly, hope it works for you. 因此,我尝试立即修改您的代码,希望它对您有用。

  jQuery(document).ready(function($) {
    var fData = new Object();
    fData.val = '';
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "php_scripts/getdeals_php.php",
        data: '{"action":"GetCats", "fData":' + JSON.stringify(fData) + '}',
        dataType: "json",
        success: function (msg)
        {
            $('#loadingmessage').hide();
            var offerList = msg;
            var Cats = document.getElementById('CategoriesSelect');
            document.getElementById("CategoriesSelect").options.length = 0;
            var optn    = document.createElement('option');
                optn.text   = "Select Category";
                optn.value  = "Select Category";        
                Cats.add(optn);


            for(var i=0;i<offerList.length;i++)
            {
                var optn    = document.createElement('option');
                optn.text   = offerList[i];
                optn.value  = offerList[i];     
                Cats.add(optn);
            }
        },
        error: function (xhr, ajaxOptions, thrownError)
        {
            alert("ERROR:" + xhr.responseText+" - "+thrownError);
        }
    });
 });

RF: AJAX in Plugins ( WP DOCS ) RF: 插件中的AJAX(WP DOCS)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM