0
JSONデータをサーバーに送信して受信しようとしています。Ajex投稿JSonレポートエラー、それを修正する方法?
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
var port = process.env.PORT || 8080;
var router = express.Router();
app.use('/api', router);
router.post('/', function (req, res)
{
res.json({ message: 'you are doing post ' });
})
app.listen(port);
console.log('Magic happens on port ' + port);
私は「郵便配達」でそれをテストしてきたし、それがうまく答え: は、ここでは私のサーバーのAPIコードです。
は、私は、HTMLのボタンにバインドされている次のコードfunction BestTourModel()
{
var self = this;
self.serverURI = 'http://localhost:8080/api';
self.ajax = function(uri, method, data)
{
var request = {
url: uri,
type: method,
contentType: "application/json",
accepts: "application/json",
cache: false,
dataType: 'json',
data: JSON.stringify(data),
success: function(data){alert(data);},
failure: function(errMsg) { alert(errMsg);}
};
return $.ajax(request);
}
self.run_algorithm = function()
{
if(self.input_value())
{
var markers = [{ "a": "11", "b": "7" },
{ "a": "44", "b": "19" },
{ "a": "45", "b": "-3" }];
self.ajax(self.serverURI(), 'POST', markers).done(function(data)
{
console.log(data); //this prints received data
});
}
}
}
var bestTourModel = new BestTourModel();
ko.applyBindings(bestTourModel, $('#main')[0]);
「run_algorithm」とクライアントからの応答を取得しようとしたとき。
<button class="btn" data-bind="click: $root.run_algorithm" >Run</button>
私が示したJSONデータを送信し、何かを受け取ることを望んで接続を確認するためにトリングたので、私は私の実際のデータを送信することができます。私はクロムデバッガでエラーが発生しました。以下に示します。
XMLHttpRequest cannot load http://localhost:8080/api. Response to preflight
request doesn't pass access control check: No 'Access-Control-Allow-Origin'
header is present on the requested resource. Origin 'null' is therefore not
allowed access.
私が間違っていることを指摘してください。私はこれに新しいです、簡単な方法で説明してください。
ありがとうございます!