如何编写使用纳克资源获取方法 [英] how to write get method using ng resource

查看:89
本文介绍了如何编写使用纳克资源获取方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

喜在我的要求,我试着用ngresource写POST方法,但现在我想改变这种状况到get方法。谁能帮助我解决这个问题。我是新来angularjs在此先感谢

  $ scope.clickresult = {};
        VAR呼叫= $资源('../ API /家庭',{},{查询:{方法:POST,IsArray的:真正}});
        $ scope.click =功能(){
            //警报(你好);
            $ scope.selected =;
            。VAR X = $('#txtSearch)VAL();
            VAR _ReqObj =新的对象();
            _ReqObj.id = X;
            _ReqObj.Meth =CD;
            //警报(X);
            Call.query({},_ReqObj,
                        功能(响应){
                            如果(响应==''){
                                //警报(无数据);
                                window.location.replace(#/);
                            }
                            其他{
                                //警报(daata);
                                $ scope.message =响应;
                            }
                        },
                                    功能(错误){
                                        window.location.replace(#/);                                    }
                                 );
        };


解决方案

下面是一些初步的帮助,这样就可以解决你自己你的未来的问题。

1。使用开发者工具,查看错误,请求和响应: https://开头开发商.google.com /网络/工具/铬devtools /

从菜单或使用CMD打开工具+ ALT Mac或CTRL + SHIFT + I在Windows + I。

在开发者工具的网络选项卡中,你可以看到你的服务器(例如请求方法= GET,来自服务器的响应)的通信。在preVIEW选项卡中可以看到服务器发送给您的JSON。告诉我,如果你有找到这个问题,因为它是非常重要的是找到你的code错误。

2。使用记录!

在角可以$日志添加到您的code和使用$ log.log(信息,对象)可以输出调试消息,并从code对象的当前状态。你可以看到在控制台选项卡中的开发工具的日志信息。

3。阅读文档

角提供的文档和示例及其功能。阅读这篇关于$资源服务 https://docs.angularjs.org/api/ngResource/service / $资源

阅读有关GET和POST方法的区别。

4。从教程尝试一个简单的例子并试图使其适应您的需求。

从互联网上复制一个简单的资源的例子,使其发挥作用。如果这样的作品改变它一步一步,直到它是你所需要的。

5。对于您的例子

请问你的服务器端脚本的工作?在你的问题,我只能看到角code。如果你想使用的服务器必须提供一个反应得到的功能GET方法。

在$资源服务已经提供了查询方法:

  {'得到':{方法:GET},
  保存:{方法:POST},
  '查询':{方法:GET,IsArray的:真正},
  删除:{方法:删除},
  删除:{方法:'删除'}};

通常你并不需要添加{查询:{方法:POST,IsArray的:真正}到你code。查询功能已经在那里!

要你只需要查询功能发送GET请求:

  VAR呼叫= $资源('../ API /家庭',{});

现在打开你的开发人员工具,进入网络选项卡,然后在你的网站上执行的函数$ scope.click。你怎么在网络选项卡看到了什么?申请应以:GET请求方法被解雇。什么是从服务器的答案吗?问题是,也许不是在你的角度code,但在你的服务器code。

尝试这些东西,并告诉我,如果你需要更多的帮助。

hi in my requirement i tried to write post method using ngresource but now i want to change that into get method. can anyone help me to solve this problem. i am new to angularjs thanks in advance

$scope.clickresult = {};
        var Call = $resource('../api/Home', {}, { query: { method: 'POST', isArray: true} });
        $scope.click = function () {
            //alert("hi");            
            $scope.selected = "";
            var x = $('#txtSearch').val();
            var _ReqObj = new Object();
            _ReqObj.id = x;
            _ReqObj.Meth = "CD";
            // alert(x);
            Call.query({}, _ReqObj,
                        function (response) {
                            if (response == '') {
                                // alert('no data');
                                window.location.replace("#/");
                            }
                            else {
                                //alert("daata");
                                $scope.message = response;
                            }
                        },
                                    function (error) {
                                        window.location.replace("#/");

                                    }
                                 );
        };

解决方案

Here is some initial help so you can solve your future problems on your own.

1. Use developer tools to see errors, requests and responses: https://developers.google.com/web/tools/chrome-devtools/

Open the tools from the menu or use cmd+alt+I on Mac or ctrl+shift+I on Windows.

In the "Network" tab of the developer tools you can see the communication with your server (E.g. request method = GET, response from the server). In the "Preview" tab you can see the json the server sent you. Tell me if you have problems finding this, because it is very important to find bugs in your code.

2. Use logging!

In angular you can add $log to your code and with $log.log("message", object) you can output debug messages and the current state of objects from your code. You can see the logging messages in the developer tools in the "Console" tab.

3. Read the documentation

Angular provides documentation and examples for their functions. Read this about the $resource service https://docs.angularjs.org/api/ngResource/service/$resource

Read about the difference between GET and POST method.

4. Try a simple example from a tutorial and try to adapt it to your needs

Copy a simple resource example from the internet and make it work. If that works change it step by step until it is what you need.

5. For your example:

How does your server side script work? In your question I can only see the angular code. If you want to use the GET method the server has to provide a function that reacts to GET.

The $resource service already provides a query method:

{ 'get':    {method:'GET'},
  'save':   {method:'POST'},
  'query':  {method:'GET', isArray:true},
  'remove': {method:'DELETE'},
  'delete': {method:'DELETE'} };

Normally you don't need to add "{ query: { method: 'POST', isArray: true}" to your code. The query function is already there!

To send a GET request with the query function you just need:

var Call = $resource('../api/Home', {});

Now open your developer tools, go to the Network tab and then execute the function $scope.click in your website. What do you see in the Network tab? The request should be fired with "request method: GET". What is the answer from the server? The problem is maybe not in your angular code but in your server code.

Try these things and tell me if you need more help.

这篇关于如何编写使用纳克资源获取方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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