简体   繁体   English

发出 http get 请求并访问我得到的数据

[英]Issuing http get request and accessing the data I'm getting

In my app, I'm issuing get request to retrieve JSON from my server.在我的应用程序中,我发出 get 请求以从我的服务器检索 JSON。

        $http.get('http://localhost:3000/documents/2.json')
            .success(
                    function(success){
                        console.log("Success");
                    })
            .error(
                    function(error){
                            console.log("error has occurred")
        });

Right now, I do successfully get 200 response, but I'm not sure how to access the json file I'm getting from the URL in my web app.现在,我确实成功获得了 200 响应,但我不确定如何访问我从 Web 应用程序中的 URL 获取的 json 文件。 I assume there's gotta be something like function(JSONData) but not sure how to implement it in my function above.我认为必须有类似function(JSONData)东西,但不确定如何在我上面的函数中实现它。

I'm issuing this in client side(Written in Angular) and getting the data from the server(written in Rails).我在客户端发出这个(用 Angular 编写)并从服务器获取数据(用 Rails 编写)。 My front end (in Angular) is part of Rails app now.我的前端(在 Angular 中)现在是 Rails 应用程序的一部分。

MAJOR EDIT:主要编辑:

This is the version (1.4.9) that the OP is using.这是 OP 使用的版本 (1.4.9)。

In the AngularJS $http Documentation v1.4.9 you get a sample script of what you are trying to do.AngularJS $http 文档 v1.4.9 中,您将获得一个示例脚本,说明您要执行的操作。

// Simple GET request example:
$http({
  method: 'GET',
  url: '/someUrl'
}).then(function successCallback(response) {
    // this callback will be called asynchronously
    // when the response is available
  }, function errorCallback(response) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
  });

inside succes call back write里面成功回调写

console.log(success)

so your code must look like this所以你的代码必须是这样的

$http.get('http://localhost:3000/documents/2.json')
            .success(
                    function(success){
                        console.log("Success");
                        console.log(success);
                    })
            .error(
                    function(error){
                            console.log("error has occurred")
        });

parameter(success) in function is data, that you receive from server, instear of success you can write anything you want .. usually i write data .. so in my case it will be function (data)函数中的参数(成功)是数据,你从服务器接收,成功后你可以写任何你想要的东西..通常我写数据..所以在我的情况下它将是函数(数据)

暂无
暂无

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

相关问题 发出同步 HTTP GET 请求或调用 JavaScript 中的 shell 脚本来自 Z1BZCBD1UID3E91 - Issuing a synchronous HTTP GET request or invoking shell script in JavaScript from iOS UIAutomation 我如何在不使用jquery发出http请求的情况下加载html文件? - How can I load an html file without issuing an http request using jquery? 使用AngularJS运行$ http POST请求时遇到404错误 - I'm getting a 404 Error when I run a $http POST request with AngularJS 实例化从发出GET请求收到的值 - Instantiate a value received from issuing a GET request 发出GET请求时节点路由不起作用 - Node Route not working when issuing a GET request 我正在尝试从angular js的http请求返回一些数据,但它给出了未定义的信息 - I'm trying to return some data from http request in angular js but it gives an undefined 我想获取我的 Graph API 令牌,但出现错误,我正在使用 HttpServices 发出 POST 请求。 我收到 400 状态码。 我正在使用 NestJS - I want to get my Graph API token but I got an error, I'm making a POST request with HttpServices. I'm getting a 400 status code. I'm using NestJS 从 Axios GET 请求访问数据 - Accessing data from Axios GET request GET 或 POST 请求,如果我从数据库中获取图像但它会增加计数器? - GET or POST request if I'm getting an image from a database but it increments a counter? 我在 Node.js API 上使用 Axios 没有收到 GET 请求的响应 - I'm getting no response of GET request with Axios on my Node.js API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM