jQuery的Ajax请求的作品,同样AngularJS Ajax请求不 [英] jQuery ajax request works, same AngularJS ajax request doesn't

查看:96
本文介绍了jQuery的Ajax请求的作品,同样AngularJS Ajax请求不的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习AngularJS,并试图建立前端系统,从Word preSS获取数据。

在后端侧似乎一切都正确设置,当我使用jQuery Ajax请求它得到的数据没有问题。

  jQuery.ajax({
    键入:POST,
    网址:/wp-admin/admin-ajax.php,
    数据: {
        动作:getdataajax
    },
    成功:功能(数据,textStatus,XMLHtt prequest){
        的console.log(数据);
    },
    错误:函数(MLHtt prequest,textStatus,errorThrown){
        执行console.log(errorThrown);
    }
});
 

但是,当我尝试做同样的事情AngularJS,这是行不通的。我试图复制与code这样的Ajax请求:

  myApp.factory(productsData',函数($ HTTP,$日志){
    返回 {
        的getProducts:功能(successcb){
            返回$ HTTP({
                方法:POST,
                网址:/wp-admin/admin-ajax.php,
                数据:{行动:getdataajax'}
            }),成功(功能(数据,状态,头,配置){
                    successcb(数据);
                    $ log.info(数据,状态,标题(),配置)

            })错误(功能(数据,状态,头,配置){
                    $ log.warn(数据,状态,标题(),配置)
            });
        },

    };
});
 

如果我记录它,它输出0。我在想什么?

感谢您的帮助。

P.S。控制器看起来是这样的:

  myApp.controller('的ProductsController,功能的ProductsController($范围,productsData){

    $ scope.sortorder ='名';

    // $ scope.products = productsData.products;
    // $ scope.products = productsData.getProducts();

    productsData.getProducts(功能(产品){
        $ scope.products =产品;
    });
});
 

解决方案

在angularjs code,使用 PARAMS:而不是数据

在jQuery的供给数据的对象:(?键1 = VAL1和放大器;键2 =值)配置设置转换为查询字符串,除非你设置过程数据:假。在angularjs,你必须使用 PARAMS:来得到一个查询字符串,数据:发送为JSON或字符串。

I'm learning AngularJS and trying to build front-end system that gets data from Wordpress.

On the back-end side everything seems to be set up properly and when I use jQuery ajax request it gets the data without problems.

jQuery.ajax({
    type: 'POST',
    url: '/wp-admin/admin-ajax.php',
    data: {
        action: 'getdataajax'
    },
    success: function(data, textStatus, XMLHttpRequest){
        console.log(data);
    },
    error: function(MLHttpRequest, textStatus, errorThrown){
        console.log(errorThrown);
    }
});

But when I try to do the same thing with AngularJS, it does not work. I'm trying to replicate the ajax request with code like this:

myApp.factory('productsData', function($http, $log) {
    return {
        getProducts: function(successcb) {
            return $http({ 
                method: 'POST', 
                url: '/wp-admin/admin-ajax.php', 
                data: {action: 'getdataajax'}
            }).success(function(data, status, headers, config) {
                    successcb(data);
                    $log.info(data, status, headers(), config)

            }).error(function(data, status, headers, config) {
                    $log.warn(data, status, headers(), config)
            });
        },

    };
});

If I log it, it outputs 0. What am I missing?

Thanks for your help.

P.S. Controller looks like this:

myApp.controller('ProductsController', function ProductsController($scope, productsData) {

    $scope.sortorder = 'name';

    // $scope.products = productsData.products;
    // $scope.products = productsData.getProducts();

    productsData.getProducts(function(products){
        $scope.products = products;
    });
});

解决方案

In the angularjs code, use params: instead of data:.

In jquery the object supplied to the data: config setting is converted to a query string (?key1=val1&key2=value2) unless you set processData: false. in angularjs, you have to use params: to get a query string, data: is sent as json or string.

这篇关于jQuery的Ajax请求的作品,同样AngularJS Ajax请求不的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆