Sto tentando di effettuare chiamate lato server a API riposanti utilizzando node.js. Restituisce con JSONP (contenitore JSON all'interno di una funzione JS) restituisce errori che sembrano essere al centro del nodo http.get (opzioni, callback) API. Can node o qualsiasi modulo restituiscono l'oggetto JSON da un ritorno JSONP?Richiesta HTTP di nodo per Restful API che restituisce JSONP
richiestarisposta
mi piacerebbe scrivere una funzione wrapper che verifica la presenza di JSON e strisce la funzione al largo della stringa restituita solo per evitare l'esecuzione di eval. Quindi JSON.parse sulla stringa (ora meno la funzione da quando l'abbiamo rimosso) per restituire json.
var request = require('request');
var getJsonFromJsonP = function (url, callback) {
request(url, function (error, response, body) {
if (!error && response.statusCode == 200) {
var jsonpData = body;
var json;
//if you don't know for sure that you are getting jsonp, then i'd do something like this
try
{
json = JSON.parse(jsonpData);
}
catch(e)
{
var startPos = jsonpData.indexOf('({');
var endPos = jsonpData.indexOf('})');
var jsonString = jsonpData.substring(startPos+1, endPos+1);
json = JSON.parse(jsonString);
}
callback(null, json);
} else {
callback(error);
}
})
}
Quindi utilizzare in questo modo:
getJsonFromJsonP('http://www.linkedin.com/countserv/count/share?url=http://techcrunch.com/2012/01/29/turning-two-founderscard-pulls-back-the-curtain-on-its-membership-community-for-entrepreneurs/', function (err, data) {
console.log('data count', data.count);
});
Eseguire il callback con vm
JavaScript code can be compiled and run immediately or compiled, saved, and run later
Una risposta precedente suggerisce striping la funzione di richiamata. Sfortunatamente, questo non è compatibile con molte risposte jsonp poiché i contenuti della funzione sono di solito oggetti e non JSON puro. La funzione JSON.parse() sarà morire per qualcosa di simile al seguente:
callback({key:"value"});
Mentre il sopra è un oggetto valido, non è JSON valido.
Di seguito eseguirà la richiamata e restituire l'oggetto:
jsonpSandbox = vm.createContext({callback: function(r){return r;}});
myObject = vm.runInContext(jsonpData,jsonpSandbox);
Quando si crea la modifica di contesto callback
al nome della funzione callback che viene restituito nella risposta jsonp.
brillante funziona perfettamente. –
- 1. Modifica intestazioni HTTP per una richiesta JSONP
- 2. HTTPS a HTTP richiesta JSONP
- 3. Richiesta http del nodo multiplo di catena
- 4. Quale richiesta HTTP RESTful per l'esecuzione di azioni sul server?
- 5. Come utilizzare un design api centrico php restful interno invece che con richiesta http
- 6. Richiesta di preflight CORS che restituisce HTTP 405
- 7. jQuery.ajax restituisce jsonp invece di JSON
- 8. API di Instagram che restituisce 400 (richiesta errata)
- 9. Esempio Richiesta JSON HTTP per Twitter API?
- 10. Alternative RESTful per ELIMINARE l'ente di richiesta
- 11. ASP.net MVC restituisce JSONP
- 12. È possibile effettuare una richiesta JSONP da HTTPS a HTTP?
- 13. API Web Convenzione di denominazione richiesta HTTP
- 14. RESTful Http DELETE in. NET
- 15. Symfony2 RESTful API + AngularJS
- 16. La richiesta JSONP fornisce 404 nell'app Angolare
- 17. Nodo vs python per la creazione di webservices RESTful
- 18. API Django e Restful
- 19. API Uber | Richiesta del token di accesso per la richiesta di Ride restituisce l'errore "invalid_grant"
- 20. Autorizzazione in API HTTP RESTful, 401 Autenticazione WWW
- 21. GitHub codice sorgente jsonp api
- 22. Restituisce JSONP nel formato corretto WCF
- 23. Utilizzo di JSONP quando si restituisce XML
- 24. RESTful API Authentication
- 25. Passa intestazioni HTTP personalizzate a richieste RESTful
- 26. La richiesta JSONP ha esito negativo quando viene utilizzato https anziché http
- 27. JSONP angolare che restituisce JSON_CALLBACK non è definito errore
- 28. CherryPy e RESTful web api
- 29. Serie di esercitazioni per API RESTful con MV Web API
- 30. come uccidere una richiesta jsonp precedente?
Riesci a capire il codice? Ho notato che non vi è alcun riferimento alla richiamata nell'URL. Di solito questo è come & callback = foo o & jsonp = bar. Sembra che LinkedIn risponda al callback. Puoi quindi utilizzare url.parse per trovare quel parametro e agire di conseguenza. – booyaa
request = require ('request'); richiesta ({uri: 'http://graph.facebook.com/?ids=http://appsumo.com'}, funzione (err, responser, body) {myObject = eval ('(' + body + ' }; – user690740
Il parametro di callback (& callback = functionName) con linkedin consente di modificare la funzione wrapper in qualsiasi nome desiderato (ma non di rimuoverlo tutti insieme) .Se potrei ottenere una risposta valida (JSON o JSONP) da e modulo o richiesta HTTP API con JS in nodo. Sarei un uomo felice. – user690740