Ho il seguente codice che funziona sta lavorando bene:Aurelia Aurelia-fetch-client e con JSON POST
import {inject} from 'aurelia-framework';
import {HttpClient, json} from 'aurelia-fetch-client';
@inject(HttpClient)
export class Items {
heading = 'Items';
apiKey = "";
constructor(http) {
http.configure(config => {
config
.useStandardConfiguration()
.withBaseUrl('https://testme.com/api/')
.withDefaults({
headers: {
'content-type': 'application/json',
'Accept': 'application/json',
'X-Requested-With': 'Fetch'
}
})
});
this.http = http;
}
attach() {
let auth = {
Username:"admin",
Password:"1234"
};
return this.http.fetch('auth', {
method: 'post',
body: JSON.stringify(auth),
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
})
.then(response => response.json())
.then(response => {
this.apiKey = response.APIKey;
console.log(response);
});
Tuttavia, se sostituire la linea body: JSON.stringify(auth)
con json(auth)
che è credo che il modo corretto di JSON serialise un oggetto che utilizza l'helper Aurelia JSON, la mia API genera una cattiva richiesta.
C'è qualcosa di diverso che l'helper fa contro JSON.stringify?
È il vostro API allo stesso dominio del sito web? In tal caso, provare a eliminare il percorso assoluto (https: //) nella funzione di configurazione. Posso vedere che a causa di qualche motivo il corpo della richiesta si svuota con URL assoluti, mentre il tuo esempio funziona bene se faccio una richiesta locale ... – Mikhail
No l'API è a un URL diverso, che in realtà mi dà un problema CORS, che ho disabilitato in Safari per ora. – user1513388
Si prega di eseguire un proxy web (ad esempio fiddler) e vedere cosa succede esattamente lì. Quindi aggiungi il risultato alla tua domanda. Hai mai visto la tua richiesta POST o forse solo una con il metodo OPTIONS? – Mikhail