I love the 'services' architecture of making requests in Angular, and wrote this little wrapper (and instructions) for my React and other JS projects.
request({
method: 'get',
url: '/path/'
}).then((resp) => {
console.log(resp);
})
Create a separate service for your API Calls:
// services/api/message.js
import request from 'shared/lib/request'
function get(id) {
return request({
url: `/message/${id}`,
method: 'GET'
});
}
function create({subject, content}) {
return request({
url: '/message/create',
method: 'POST',
data: {
subject,
content
}
});
}
const MessageService = {
get, create //, update, delete, etc. ...
}
export default MessageService;
Now call the methods from your module/view/component:
import React from 'react'
import MessageService from 'services/api/message'
class Message extends React.Component {
handleSubmit() {
const {subject, message} = this.state;
MessageService
.create({subject, message})
.then((response) => {
alert(`New Message with id ${response.id} created!`);
});
}
// Other stuff...
}
@ahsanihsan can you show us your request ?