1

Here is my app.js:

var loginModule = angular.module('loginModule',[]);
loginModule.config(['$httpProvider', function ($httpProvider) {    
    $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';
}]);
loginModule.controller('loginCtrl',function ($scope,$http,$parse){
    $scope.signin_user = {name:"",password:""};
    $scope.errors = [];
    $scope.submit = function(){
        if($scope.login_form.$valid){
            var data ="name="+$scope.signin_user.name+"&password="+$scope.signin_user.password;
            $http({
                method:'post',
                url:'${ctx}/login',
                data:data
            })
            .success(function(data,status){
                console.log(data);
            })
            .error(function(data,status){
                console.log('status:'+status);
            });
        }
    };
});

In login.jsp I'm using jstl to get my web root,here is my tag.jsp, in login.jsp just include this file,which can use ${ctx} to get the web root.

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="ctx" value="${pageContext.request.contextPath }" ></c:set>

But I don't understand what happend in the angularjs, using $htpp.post(url:'${ctx}/login'),just like app.js file.
And I use chrome developer tools to watch request url,I find the request url like this picture: https://i.sstatic.net/eWgs2.png the url was append other unreadable code.
I hope someone can fix it,thank all. If you can't see the picture, I would tell you in chrome developer tools the request url is 'localhost:8080/cloudaction/$%7Bctx%7D/login '

2 Answers 2

0

I guess the content type on both side should be same then It would work. Try from angularjs side setting content-type as "text/html" or both side "application/json".

Sign up to request clarification or add additional context in comments.

Comments

0

The issue here is that you cannot access to ${ctx} in your angular controller. In fact, the ${ctx} in your angular url is encoded in $%7Bctx%7D because angular don't know that it is a variable ( it is simply a string). I think this post should be able to help you : Using a Relative Path for a Service Call in AngularJS

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.