I have written and angular js program where I am doing a get Request and trying to fetch a resource from a spring Boot rest service. But I am getting this error :
No 'Access-Control-Allow-Origin' header is present on the requested resource
In Angular Js :
ctrl.js
(function(){
"use strict";
angular.module("app")
.controller("chatCtrl",function($scope,$http){
$scope.name = "ujjawal";
$scope.usermessages = [];
$scope.botmessages = [];
$scope.onSend = onSend;
function onSend(){
var usersegment = {
"text":this.msg
}
this.msg = "";
this.usermessages.push(usersegment);
$http.get("http://localhost:8080/messages/").then(function(response){
console.log(response.data);
var botsegment = {
"id":response.data.id,
"text":response.data.text
}
$scope.botmessages.push(botsegment);
});
}
});
})();
In Spring Boot :
MessagesController
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MessagesController {
private int id = 0;
@RequestMapping("/messages")
public Messages getMessages(){
id+=1;
return new Messages(Integer.toString(id),"Bot is replying hi");
}
}
Messages.java
public class Messages {
private String id;
private String messages;
public Messages(String id, String messages) {
this.id = id;
this.messages = messages;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getMessages() {
return messages;
}
public void setMessages(String messages) {
this.messages = messages;
}
}
It looks like I need to pass a header "Access-Control-Allow-Origin" but I don't know how. I saw a lot of sites and docs but didn't get it. Please help.