解决方法如下:
完整的代码如下(遇到类型问题的同学可以做个参考):
复制代码 代码如下:
var querystring = require('querystring')
, http = require('http')
var data = querystring.stringify({
info:'hi',
test:5
})
var opt = {
hostname:'www.test.com',
port :9094,
path:'/perationSqlQuery',
method: 'POST',
headers: {
'Content-Type':'application/x-www-form-urlencoded',
'Content-Length': data.length
}
}
var req = http.request(opt, function (res) {
res.on('data', function (data) {
console.log(data.toString())
})
})
req.on('error', function(e) {
console.log('problem with request: ' + e.message)
})
req.write(data)
req.end()
Vue.js构造函数的选项对象中,最基本的选项包括:
el:指定Vue实例控制的HTML元素的选择器。
data:指定Vue实例使用的数据对象。
methods:指定Vue实例的方法。
computed:指定Vue实例的计算属性。
watch:指定Vue实例的监听器。
例如,下面的代码演示了如何使用这些选项:
const app = new Vue({
el: '#app',
data: {
message: 'Hello, world!'
},
methods: {
reverseMessage() {
this.message = this.message.split('').reverse().join('')
}
},
computed: {
reversedMessage: {
get() {
return this.message.split('').reverse().join('')
},
set(value) {
this.message = value.split('').reverse().join('')
}
}
},
watch: {
message(newValue, oldValue) {
console.log(`message changed from ${oldValue} to ${newValue}`)
}
}
})
方法是利用vue-resource组件提供的一系列api:get(url, [data], [success], [options])
post(url, [data], [success], [options])
put(url, [data], [success], [options])
patch(url, [data], [success], [options])
delete(url, [data], [success], [options])
jsonp(url, [data], [success], [options])
具体举例如下:
1、导入vue-resource
<script src="js/vue.js"></script>
<script src="js/vue-resource.js"></script>
2、基于全局Vue对象使用http
// 通过someUrl获取后台数据,成功后执行then的代码
Vue.http.get('/someUrl', [options]).then(successCallback, errorCallback)
3、在一个Vue实例内使用$http
// $http是在vue的局部范围内的实例
this.$http.get('/someUrl', [options]).then(successCallback, errorCallback)
说明:
在发送请求后,使用then方法来处理响应结果,then方法有两个参数,第一个参数是响应成功时的回调函数,第二个参数是响应失败时的回调函数。