Ho creato un'API minimalista su nodejs che restituiscono i dati in formato JSON.Il token di token imprevisto JSON dopo jQuery.ajax # get
Ma ogni volta che provo a fare una chiamata ajax # e passare la mia API come URL, ricevo un errore e giudicando da Chrome, sto ricevendo un errore "Unexpected token :"
;
qui il codice del server in nodejs + esprimono:
var
http = require('http'),
express = require('express'),
app = express(),
server = http.createServer(app);
app.get('/', function(req, res) {
console.log('req received');
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({
Name : "Tom",
Description : "Hello it's me!"
}));
});
server.listen(3000, function() {
console.log('Listening on 3000');
});
Il JSON restituito da "/"
è: {"Name":"Tom","Description":"Hello it's me!"}
.
Ecco la mia chiamata dai js client:
$.ajax({
url: findUrl,
type: 'get',
dataType: 'jsonp',
success: function (data) {
self.name(data.Name);
self.description(data.Description);
},
error: function(jqXHR, textStatus, errorThrown) {
alert(errorThrown);
}
});
Nel tracciare l'errore che ottengo: "jQuery111108398571682628244_1403193212453 was not called"
qualcuno può aiutarmi?
So che questa domanda è stata già posta ma non riesco a trovare una soluzione che risolva il mio programma.
Controlla il tuo 'dataType '- [$ .ajax()] (http://api.jquery.com/jquery.ajax/) – Andreas
@Andreas: Immagino che tu ti riferisca a" jsonp ", l'ho cambiato in" json ". Ora sto ottenendo "XMLHttpRequest non può caricare http: // localhost: 3000 /. Nessuna intestazione 'Access-Control-Allow-Origin' è presente sulla risorsa richiesta. L'origine 'null' non è quindi consentita l'accesso." come posso risolvere questo? –