Today, the front-end debugging, because the interface is currently and front-end is not a machine, so the front end of the use of Ajax access interface needs to cross the domain;
Preparatory work:
1, the use is Cros
2, the front-end use of jquery Ajax;
3, back-end node + Express
Steps:
1, front-end configuration:
$.ajax (
type: "POST",
URL: "http://xxxxxx",
dataType: ' JSON '
data: {name: "John", Location: "Boston"}
xhrfields:
' access-control-allow-origin ': ' * ' (
msg)->
console.log "Data Saved:" + msg
false
). Fail ((JQXHR, textstatus)->
console.log "fail:" + Textstatus
false
)
2, Back-end configuration:
Router.post ('/create ', function (req, res) {
var db = req.db;
Res.setheader (' Access-control-allow-origin ', ' * ');
var user = {
username: ' The reddest ',
email: ' brandon@switchonthecode.com ',
firstName: ' Brandon '
, LastName: ' Cannaday '
};
var userstring = json.stringify (user);
Res.send (userstring);
});
Summarize:
1, the front-end needs to specify DataType and Access-control-allow-origin
DataType: ' JSON '
data: {name: "John", Location: "Boston"}
xhrfields: '
access-control-allow-origin ': ' *'
2, back-end needs to configure Access-control-allow-origin and results into JSON
Es.setheader (' Access-control-allow-origin ', ' * ');
var user = {
username: ' The reddest ',
email: ' brandon@switchonthecode.com ',
firstName: ' Brandon '
, LastName: ' Cannaday '
};
var userstring = json.stringify (user);
Reference:
1, HTTPS://DEVELOPER.CHROME.COM/EXTENSIONS/XHR
2, http://www.html5rocks.com/en/tutorials/cors/