[英]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.