Ich habe eine Node.js (Express) Webapp läuft auf localhost: 3000. Ich versuche jetzt eine mobile App mit Cordova zu entwickeln.Cordova App Ajax Anruf an localhost schlägt fehl. :: ERR_CONNECTION_REFUSED
Ich habe eine Route in meiner Express-App 'localhost: 3000/tweets' definiert, die beim Aufruf ein paar Tweets mit der twitter-API bekommt und sie als json-Objekt zurückschickt. Everythinggs funktioniert sehr gut mit Web-App, aber ich habe Mühe, den gleichen Ajax-Anruf von einer mobilen App zu machen. Damit Anfragen von anderen Hosts ich das meinem edxpress app.js hinzugefügt haben: EDIT: Ich benutze cors Middleware jetzt:
app.use(cors());
In meiner cordova App:
Meta-Tag:
<meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'">
config.xml
<?xml version='1.0' encoding='utf-8'?>
<widget id="<id...>" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
<name>appname</name>
<description>
A twitter search app.
</description>
<author email="[email protected]" href="http://cordova.io">
Apache Cordova Team
</author>
<content src="index.html" />
<access origin="*" />
<allow-intent href="http://*/*" />
<allow-intent href="https://*/*" />
<allow-intent href="tel:*" />
<allow-intent href="sms:*" />
<allow-intent href="mailto:*" />
<allow-intent href="geo:*" />
<platform name="android">
<access origin="*" />
<allow-intent href="market:*" />
</platform>
<platform name="ios">
<allow-intent href="itms:*" />
<allow-intent href="itms-apps:*" />
</platform>
<engine name="android" spec="^6.2.3" />
<plugin name="cordova-plugin-camera" spec="^2.4.1" />
<plugin name="cordova-plugin-whitelist" spec="^1.3.2" />
</widget>
In der Datei index.html link i fi 'querySender.js' le an der Unterseite des Körpers:
<script type="text/javascript" src="js/querySender.js"></script>
Und schließlich Inhalt von 'querySedner.js'
$(document).ready(function(){
$('#btn_send').on('click', function(){
console.log("app is now sending query!");
$.ajax({
type: 'POST',
url: 'http://127.0.0.1:3000/tweets',
data: {name:"test_name",lastname:"last_name"},
dataType:'json',
success: function(dataR){
var date = new Date();
console.log("POST success recorded at: ",date.getTime());
if (!dataR){
console.log("No Data received");
}else{
console.log("DataR: ",dataR);
}
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
console.log("Status: " + textStatus+" error:"+ errorThrown);
},
timeout:5000
});
});
});
mit WebStrom IDE Ich habe versucht index.html in Chrom (es läuft es auf localhost auszuführen: 63342) Die Anfrage ist erfolgreich und die Daten werden wie erwartet zugestellt.
jedoch ist die App mit Android Emulator emulieren, wenn ich drücken Sie die Taste i erhalten:
POST http://127.0.0.1:3000/tweets net::ERR_CONNECTION_REFUSED -- jquery-3.2.1.min.js:4
Grundsätzlich. Ich betreibe meinen Node.js (Express) -Server lokal und möchte Ajax-Anrufe von der Cordova-App auf dem Android-Emulator ausführen. Was habe ich falsch verstanden?
hast du versucht url: 'localhost: 3000/tweets'' statt "url:' http: //127.0.0.1: 3000/tweets''? Veröffentlichen Sie auch Ihren Express-Code für die gleiche –
ja, ohne Erfolg. Ohne die 'http: //' bekomme ich das bei der Ausführung in einem Browser: Fehler: SecurityError: 'open' auf 'XMLHttpRequest' konnte nicht ausgeführt werden: Verbindung zu 'localhost: 3000/tweets' wurde abgelehnt, da es den Inhalt des Dokuments verletzt Sicherheitsrichtlinie. und auf Android Emualtor läuft produziert: jquery-3.2.1.min.js: 4 POST localhost: 3000/Tweets 404 – EasternDude
(nicht gefunden) cors Modul von Express.js installieren Cross-Domain-Anforderung ordnungsgemäß zu ermöglichen, können Sie folgen [ dies] (http://stackoverflow.com/questions/7067966/how-to-allow-cors#37845319) –