简体   繁体   English

在WordPress上使用AngularJS $ http不会响应预期

[英]Using AngularJS $http with WordPress doesn't respond as expected

I've done a wp_localize_script for my ajax in wordpress: 我已经在wordpress中为我的ajax完成了wp_localize_script

wp_localize_script('cb_admin_js', 'cbAjax', array('ajax_url' => admin_url( 'admin-ajax.php' )));

In angular I've doing an $http.get() to perform a test: 在角度我做了$http.get()来执行测试:

$http.get('http://localhost/wp-admin/admin-ajax.php',{action:'get_saved_cover'}).then(function(data){
        console.log(data);
    });

My function should just return a set of cars: 我的函数应该只返回一组汽车:

add_action( 'wp_ajax_nopriv_get_saved_cover', 'get_saved_cover' );
add_action( 'wp_ajax_get_saved_cover', 'get_saved_cover' );

function get_saved_cover(){
    $cars=array("Volvo","BMW","Toyota");
    return json_encode($cars);
    wp_die();
}

I'm only getting back data: 0 , status: 200 , and a few other standard responses in an array. 我只得到数组中的data: 0status: 200和其他一些标准响应。 I'm not sure I'm missing a piece that Angular wants, or what. 我不确定我是否缺少Angular想要的东西,或者想要什么。

Change the angular http argument to params instead of data 将angular http参数更改为params而不是data

 $http({
    url: 'http://localhost/wp-admin/admin-ajax.php',
    method: "GET",
    params: {action: "get_saved_cover"}
}).then(function (response) {
   console.log(response.data);
});

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

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