Skip to content

Commit c5963d2

Browse files
committed
added jquery to package.json
debugged and fixed Access-Control-Allow-Origin in api added api.js file with jquery ajax call to get todo tasks added constants.js file
1 parent afc344b commit c5963d2

File tree

4 files changed

+42
-5
lines changed

4 files changed

+42
-5
lines changed

ep19-call-api-jquery/app/components/App.jsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@ import React from 'react';
22
import DisplayList from './DisplayList';
33

44
var rand = require('random-key');
5+
var api = require("../utils/api");
56

67
export default class App extends React.Component {
78

89
constructor () {
910
super();
10-
this.state = { title: '', todos: [
11-
{ title: 'eggs', done: false, id: 1 },
12-
{ title: 'banana', done: false, id: 2 },
13-
{ title: 'bread', done: false, id: 3 }
14-
] };
11+
this.state = { title: '', todos: [] };
12+
var processData = function(data) {
13+
this.setState({todos: data.todos});
14+
};
15+
api.getTasks(processData.bind(this));
1516
}
1617

1718
handleDone (idToBeMarkedAsDone) {

ep19-call-api-jquery/app/utils/api.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
var Constants = require("./constants");
2+
var $ = require('jquery');
3+
4+
var api = {
5+
getTasks (processData) {
6+
var url = Constants.BASE_URL + 'todos';
7+
this.makeAjaxCall(url, 'GET', processData)
8+
},
9+
10+
makeAjaxCall (url, type, processDataCallback) {
11+
$.ajax({
12+
type: type,
13+
url: url,
14+
data: {
15+
api_key: Constants.API_KEY
16+
},
17+
dataType: 'json',
18+
success: function(data) {
19+
console.log(data);
20+
processDataCallback(data);
21+
},
22+
error: function() {
23+
console.log("An error has occurred");
24+
}
25+
});
26+
}
27+
};
28+
29+
module.exports = api;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
var Constants = {
2+
BASE_URL: "http://localhost:3000/api/v1/",
3+
API_KEY: "93d2612b-20df-42e3-89fa-6af7889be7e5"
4+
};
5+
6+
module.exports = Constants;

ep19-call-api-jquery/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"webpack-merge": "^0.1.2"
2424
},
2525
"dependencies": {
26+
"jquery": "1.11.3",
2627
"random-key": "^0.3.2",
2728
"react": "^0.13.3"
2829
}

0 commit comments

Comments
 (0)