私は単純なNode.jsサーバーを稼働させています。これはコードです:Vue.jsでNode.jsサーバーを呼び出す
var http = require('http');
var server = http.createServer();
server.on('request', function(req, res) {
res.writeHead(200, {
'content-type': 'text/plain'
});
res.write('Hello World!');
res.end();
})
server.listen(8090);
server.once('listening', function() {
console.log('Hello World server listening on port %d', 8090);
});
私は、コマンドラインからカールを使用して、このサーバーを呼び出すことができます。私はVueのアプリケーションから呼び出すしようとすると、
$curl localhost:8090
はしかし、私はエラーを取得します。 localhost:8080で実行されているVueアプリケーションがあり、localhost:8090サーバーを呼び出すとします。マイmain.jsのVueのファイルはこれです:
import Vue from 'vue'
import resources from 'vue-resource'
Vue.use(resources)
import App from './components/App.vue'
import style from './styles/main.scss'
/**
* Root Vue instance
* @param {[element]} el: 'body' [Adds to the html body]
* @param {[component]} components: {app: App} [Renders ./component/App]
*/
new Vue({
el: 'body',
components: {
app: App
}
})
をそして、これはアプリケーションコンポーネントです:
XMLHttpRequest cannot load localhost:8090. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.
私は試してみてください。私はボタンをクリックすると
<template>
<h1>{{ msg }}</h1>
<input v-model="msg">
<button v-on:click="get">Call Server</button>
</template>
<script>
export default {
data: function() {
return {
msg: 'Hello World!'
}
},
methods: {
get: function() {
// GET request
this.$http({
url: 'localhost:8090',
method: 'GET'
}).then(function(response) {
console.log('ok');
}, function(response) {
console.log('failed');
});
}
}
}
</script>
私はこのエラーを取得しますgoogle.comのような別のサーバーを呼び出すと、次のエラーが表示されます。
build.js:19188 GET http://localhost:8080/google.com 404 (Not Found)
それで、Vueは呼び出しの前にlocalhost:8080を置いているようですが、これは私の問題がある場所でしょうか?サーバコールをするのは全く新しいものです。私はVueで遊んでいて、Node.jsを学びたいと思っています。
http:// www.google.comやhttp:// localhost:8090 – Molda
のような完全なアドレスを使用するようにしてください.Mouldaによると、Googleのクエリに現在のプロトコルスキームを接頭辞として付ける必要があります。あなたのリクエストは、現在ページを提供しているサーバーのルートからの相対的なものです。 – oligofren