Click here to Skip to main content
15,314,675 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to get string from java method using AJAX request($http GET) and then to show it in my html using AngularJS, but I can't access my java function to retrieve information from it. It gives me 404 not found error every time. If someone can give me some example of how to do it or change my code to work it would be really helpful.

My html:
HTML
<pre><!DOCTYPE html>
<html>
<head>

<title>Proba</title>
<script src="js/angular.js"></script>
<script src="js/angular-route.js"></script>
<script type="text/javascript" src="js/Proba.js"></script>

</head>

<body ng-app="app" ng-controller="kontroler">

	<h1>Probavam po stoti put ovo sranje!!!</h1>
	<h2>{{hello}}</h2>
	<h2>{{kakobilo}}</h2>

</body>

</html>

My Angluar:
JavaScript
var app = angular.module("app",[]);

app.controller('kontroler',['$scope','$http',function($scope,$http){
	$scope.hello = "Hello World!";
	$scope.helloFunction = function(){
		$http({
			url: 'rest/servisi/ime',
			method: 'GET'
		}).then(function(resp){
			$scope.bilokako = resp.data;
		},function(resp){
			
		});
	};
	$scope.helloFunction();
}]);


My java function:

Java
package Servisi;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

@Path("/servisi")
public class Name {
	
	@GET
	@Path("/ime")
	@Produces(MediaType.TEXT_PLAIN)
	public String Naziv() {
		return "Hello World from Function !!!"; 
	}
}


My XML:
HTML
<pre><?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>Proba</display-name>
  <welcome-file-list>
    <welcome-file>Proba.html</welcome-file>
  </welcome-file-list>
  <servlet>
		<servlet-name>Jersey REST Service</servlet-name>
		<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
		<init-param>
			<param-name>jersey.config.server.provider.packages</param-name>
			<param-value>services,org.codehaus.jackson.jaxrs</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>Jersey REST Service</servlet-name>
		<!-- Bazna putanja do REST servisa. Može biti bilo šta (pa i prazan string) -->
		<url-pattern>/rest/*</url-pattern>
	</servlet-mapping>
</web-app>


What I have tried:

I give you my example in which I tried to access my information from java method, but I can't retrieve it with REST in AJAX request.
Posted
Comments
sajeetharan 22-Jul-18 9:08am
   
are you getting the data in the console?
Member 13810592 22-Jul-18 9:25am
   
Nope, i dont have anything in console...
Member 13810592 22-Jul-18 9:27am
   
http://prntscr.com/k9irnh
sajeetharan 22-Jul-18 9:45am
   
screenshot was removed
Member 13810592 22-Jul-18 9:50am
   
hmm i have it let`s try here : https://www.sendspace.com/file/fxp8nh
Member 13810592 22-Jul-18 9:55am
   
https://www.sendspace.com/file/ove3xh this is when i try to access field bilokako...
sajeetharan 22-Jul-18 15:14pm
   
thats not how you access data. tell me what do you see when you put console.log(JSON.stringify($scope.bilokako));
Member 13810592 22-Jul-18 16:51pm
   
https://www.sendspace.com/file/yb4ona
sajeetharan 23-Jul-18 0:38am
   
please contact me via email sajeetharan@outlook.com
Member 13810592 23-Jul-18 14:18pm
   
I send you a message.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900