简体   繁体   中英

Trouble using POST with core-ajax-dart to send JSON data to a server REST API

I am having trouble sending JSON data to my custom REST sever using core-ajax-dart. I can sucessfully send JSON data with the following

var host = window.location.hostname;
var data = JSON.encode(rows);
HttpRequest.request('http://$host:9999/services/directory/items/$instanceId', method: 'POST', mimeType: 'application/json', sendData: data).catchError((obj) {
  print(obj);
}).then((HttpRequest val) {
  print('response: ${val.responseText}');
}, onError: (e) => print("error"));

but when I try and do the same thing with core-ajax-dart,

<core-ajax-dart id="ajax"
  response="{{ response }}"
  handleAs="json"></core-ajax-dart>

$['ajax']
  ..url="http://$host:9999/services/directory/items/$instanceId"
  ..method = 'POST'
  ..contentType = 'application/json'
  ..body = JSON.encode(rows)
  ..go()
  ;

it fails with

Failed to load resource: the server responded with a status of 405 (Method Not Allowed)

Can someone tell me what I'm doing wrong here, as as far as I can see the core-ajax-dart method should work as advertised?

CLARIFICATION: I'm already using CORS, and the first HttpRequest.request works fine, just not when trying to POST using core-ajax-dart

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM