java - AngularJS and SparkJava. Never enter into .success() method and can't see server's response -
i'm starting out angularjs , client-server programming overall. have angularjs client , java server (made sparkjava framework).
this snippet of controller's code:
var urlshortener = angular.module('urlshortener', []); urlshortener.controller('shortenctrl', function($scope, $http){ $scope.longurl = ""; $scope.shortening = function(longurl){ $http.get("http://localhost:4567/shortening", {params:{longurl: longurl}}) .success(function(response){ alert("success"); }) .error(function(response){ alert("error"); }) } }
);
...and maven project's server code
import static spark.spark.*; public class server { public static void main(string[] args) { get("/shortening", (request, response) -> { string longurl = request.queryparams("longurl"); system.out.println(longurl); //this works fine return "yo"; }); } }
i'm checking stuffs work well, don't. every time try send data client server works fine. in end, when server send response client, on screen appears alert "error", .success() it's never called.
why? can me? thanks.
if testing google chrome, please press f12, click 'console' , execute action calls controller's function. facing following error, isn't ?
no 'access-control-allow-origin' header present on requested resource. origin 'null' therefore not allowed access
if that's case, de following:
1: in java code, set static file location (insert line below first instruction main method):
externalstaticfilelocation("<complete-path-to-the-directory-in-which-your-html-page-is>");
2: in javascript code, replace "http://localhost:4567/shortening" "/shortening"
3: instead of double-clicking page file in order open in browser, open browser , access "http://localhost:4567/yourpagename.html"
Comments
Post a Comment