这里是主要的。我的vue应用程序的js,此应用程序正在localhost:8080上运行,并尝试将数据发布到localhost:8081(express.js)
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import axios from './backend/vue-axios'
import BootstrapVue from 'bootstrap-vue'
import 'bootstrap/dist/css/bootstrap.css'
import 'bootstrap-vue/dist/bootstrap-vue.css'
Vue.use(BootstrapVue)
Vue.config.productionTip = false
/* eslint-disable no-new */
new Vue({
el: '#app',
router,
axios,
template: '<App/>',
components: { App }
})
login.vue组件
<template>
<div class="login-wrapper border border-light">
<form class="form-signin" @submit.prevent="login">
<h2 class="form-signin-heading">Please sign in</h2>
<label for="inputEmail" class="sr-only">Email address</label>
<input v-model="email" type="email" id="inputEmail" class="form-control" placeholder="Email address" required autofocus>
<label for="inputPassword" class="sr-only">Password</label>
<input v-model="password" type="password" id="inputPassword" class="form-control" placeholder="Password" required>
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
</form>
</div>
</template>
<script>
export default {
name: 'Login',
data () {
return {
email: '',
password: ''
}
},
methods: {
login () {
this.axios.post('http://localhost:8081/login', { username: this.email, password: this.password, grant_type: 'password', client_id: null, client_secret: null })
.then(request => this.loginSuccessful(request))
.catch(() => this.loginFailed())
},
loginSuccessful (req) {
if (!req.data.token) {
this.loginFailed()
return
}
localStorage.token = req.data.token
alert(req.data.token)
this.error = false
this.$router.replace(this.$route.query.redirect || '/authors')
},
loginFailed () {
this.error = 'Login failed!'
delete localStorage.token
}
}
}
</script>
<style lang="css">
body {
background: #605B56;
}
.login-wrapper {
background: #fff;
width: 70%;
margin: 12% auto;
}
.form-signin {
max-width: 330px;
padding: 10% 15px;
margin: 0 auto;
}
.form-signin .form-signin-heading,
.form-signin .checkbox {
margin-bottom: 10px;
}
.form-signin .checkbox {
font-weight: normal;
}
.form-signin .form-control {
position: relative;
height: auto;
-webkit-box-sizing: border-box;
box-sizing: border-box;
padding: 10px;
font-size: 16px;
}
.form-signin .form-control:focus {
z-index: 2;
}
.form-signin input[type="email"] {
margin-bottom: -1px;
border-bottom-right-radius: 0;
border-bottom-left-radius: 0;
}
.form-signin input[type="password"] {
margin-bottom: 10px;
border-top-left-radius: 0;
border-top-right-radius: 0;
}
</style>
/src/backend/vue-axios/axios。js公司
import axios from 'axios'
const API_URL = 'http://localhost:8081/restrictedArea/enter'
export default axios.create({
baseURL: API_URL,
headers: {
'content-type': 'application/x-www-form-urlencoded',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, PATCH, PUT, DELETE, OPTIONS',
'Access-Control-Allow-Headers': 'Origin, Content-Type, X-Auth-Token'
// 'Authorization': 'Bearer ' + localStorage.token
}
})
/src/backend/vue-axios/index.js
import Vue from 'vue'
import VueAxios from 'vue-axios'
import axios from './axios'
Vue.use(VueAxios, axios)
export default axios
指数js(后端express.js)
//MARK: --- REQUIRE MODULES
const port = 8081
const mySqlConnection = require('./databaseHelpers/mySqlWrapper')
const accessTokenDBHelper = require('./databaseHelpers/accessTokensDBHelper')(mySqlConnection)
const userDBHelper = require('./databaseHelpers/userDBHelper')(mySqlConnection)
const oAuthModel = require('./authorisation/accessTokenModel')(userDBHelper, accessTokenDBHelper)
const oAuth2Server = require('node-oauth2-server')
const express = require('express')
const expressApp = express()
var cors = require('cors')
expressApp.use(cors())
expressApp.oauth = oAuth2Server({
model: oAuthModel,
grants: ['password'],
debug: true
})
const restrictedAreaRoutesMethods = require('./restrictedArea/restrictedAreaRoutesMethods.js')
const restrictedAreaRoutes = require('./restrictedArea/restrictedAreaRoutes.js')(express.Router(), expressApp, restrictedAreaRoutesMethods)
const authRoutesMethods = require('./authorisation/authRoutesMethods')(userDBHelper)
const authRoutes = require('./authorisation/authRoutes')(express.Router(), expressApp, authRoutesMethods)
const bodyParser = require('body-parser')
//MARK: --- REQUIRE MODULES
//MARK: --- INITIALISE MIDDLEWARE & ROUTES
//set the bodyParser to parse the urlencoded post data
expressApp.use(bodyParser.urlencoded({ extended: true }))
//set the oAuth errorHandler
expressApp.use(expressApp.oauth.errorHandler())
//set the authRoutes for registration and & login requests
expressApp.use('/auth', authRoutes)
//set the restrictedAreaRoutes used to demo the accesiblity or routes that ar OAuth2 protected
expressApp.use('/restrictedArea', restrictedAreaRoutes)
//MARK: --- INITIALISE MIDDLEWARE & ROUTES
var allowCrossDomain = function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
// intercept OPTIONS method
if ('OPTIONS' == req.method) {
res.sendStatus(200);
} else {
next();
}
};
expressApp.use(allowCrossDomain);
//init the server
expressApp.listen(port, () => {
console.log(`listening on port ${port}`)
})
expressApp.post('/login',function(req,res){
var username=req.body.username;
var password=req.body.password;
console.log("User name1 = "+username+", password is "+password);
res.end("yes");
});
控制台输出:
listening on port 8081
User name1 = undefined, password is undefined
User name1 = undefined, password is undefined
Axios以这种格式发布,我在chrome控制台中找到了它:{"用户名":"hkvega01@gmail.com","密码":"123","grant_type":"密码","client_id": null,"client_secret": null}:
我使用chrome ARC发布的格式是成功的:用户名=测试
有人知道问题出在哪里吗?
在main试试这个。js公司:
expressApp.use(bodyParser.json());
此外,在axios中不使用url编码。js,使用json。将标题改为:
'content-type': 'application/json',
问题内容: 我必须承认我有点困惑…我以前从未做过,而且我显然缺少一些东西 当我通过http.post将数据传递到我的php文件时,我似乎无法收集数据… 有人可以告诉我为什么这行不通吗? FormData会显示在控制台日志中,并且可以确定正在写入文件。但是,看起来好像没有数据传递。 这是在我的php文件中。试图将提交表单中的数据写入文件中(只是测试)。 问题答案: 经过大量研究后,我发现这有点像ph
试图将$http.post请求从AngularJS表单发送到Symfony控制器以将表单内容添加到数据库。我可以在AngularJS侧获得"状态"200的成功响应。 但是,在Symfony控制器上,$request- 另外,安装了FOSRestBundle并启用了body listener、param fetcher listener。 我知道我的问题与AngularJS对Symfony控制器的P
我正在尝试使用axios发送数据,但它发送的响应超出预期。当我使用postman发出相同请求时,它会成功地向我的手机发送通知,以下是postman的响应: 但是使用axios,通知不会发送到我的手机,以下是axios的响应: 这是我的axios代码:
我有一个仪表板活动有3个片段,最初它是调用一个家庭片段,我想显示一个布局当我收到一个互联网的广播没有连接,但我的布局总是显示空指针异常...一开始互联网布局的可见性消失了。 当我设置我的可见性,然后它显示和重叠我的片段设计,但一旦设置它的可见性消失,然后想要显示它总是显示错误 为什么会发生这种情况 checknet.java dashboard.xml
我正在尝试spring websockets,由于某些原因我不明白,我可以建立与服务器的连接,但当我发送数据时什么也没有发生。 下面是我的配置类(与其他spring websocket示例完全相同): } 我的控制器,在一个包中,我确保spring inicialites它,因为我用@PostConstruct注释说明init()消息。如您所见,我编写了system.out.println以便在控
我想知道过去2天在这里和那里。我的问题是,我正在使用多部分/表单数据发送多个文件,其中包含一些文本/纯字段。问题是,当我使用发送数据时,它工作正常,但是当我尝试使用发送数据时,服务器没有收到任何东西,下面是我的, 下面是方法,我是如何使用这个类的, 下面是我ASP的一面, 请帮我解决这个问题…!!!提前感谢。