我已经在端口8080 上运行了rest api
。我创建了express project
,它运行在端口3000 上。
如何调用rest api
来获取数据?
答案 0 :(得分:0)
我的app.js是
/**
* Module dependencies.
*/
var express = require('express')
, routes = require('./routes')
, user = require('./routes/user')
, http = require('http')
, path = require('path');
var bodyParser = require("body-parser");
var jquery = require('jquery');
var app = express();
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.get('/', routes.index);
app.get('/users', user.list);
app.post('/login', function(req, res){
console.log("User name = "+req.body.email+", password is "+req.body.password);
var data = {
email:req.body.email,
password:req.body.password
};
jquery.ajax({
type: 'POST',
data: JSON.stringify(data),
contentType: 'application/json',
dataType:"json",
url: 'http://localhost:8080/LT/login',
success: function(data) {
console.log('success');
console.log(JSON.stringify(data));
if(data.accessToken!='')
{
alert("login success");
}
},
error:function(data){
console.log('error');
console.log(data);
}
});
console.log('Vishwas');
});
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
我的index.ejs是
<html lang="en">
<% include header %>
<body>
<% include banner %>
<div class="container">
<div id="login-page" class="row">
<div class="col m4 z-depth-6 card-panel offset-m4">
<!-- <form class="login-form" action="login.htm" method="post"> -->
<div class="row">
<div class="input-field col m12" >
<p class="center login-form-text"> Login Form</p>
</div>
</div>
<div class="row margin">
<div class="input-field col m12" >
<i class="mdi-social-person-outline prefix"></i>
<input class="validate" id="email" type="email">
<label for="email" data-error="wrong" data-success="right" class="center-align">Email</label>
</div>
</div>
<div class="row margin">
<div class="input-field col m12" >
<i class="mdi-action-lock-outline prefix"></i>
<input id="password" type="password">
<label for="password">password</label>
</div>
</div>
<div class="row">
<div class="input-field col m12 login-text">
<input type="checkbox" id="remember-me" />
<label for="remember-me">Remember me</label>
</div>
</div>
<div class="row">
<div class="input-field col m12">
<button id="send" class="btn waves-effect waves-light col s12" style="text-align:center">Login</button>
</div>
</div>
<div class="input-field col m6">
<p class="margin right-align medium-small"><a href="forgot-password.html">Forgot password?</a></p>
</div>
</div>
<!-- </form> -->
</div>
</div>
<% include footer %>
<!-- Scripts-->
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="js/materialize.js"></script>
<script src="js/init.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#send').click(function(e){
e.preventDefault();
//console.log('seend_link clicked');
var data = {
email:"ashishg@dewsolutions.in",
password:"password"
};
$.post("http://localhost:3000/login",{email:"ashishg@dewsolutions.in",password:"password"},function(data){
console.log(data);
});
});
});
</script>
</body>
</html>