1

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

James Hurford
  • 2,018
  • 19
  • 40

1 Answers1

0

Seems you need to set CORS headers on the server.

see
- Client in DART comunicate with server in PHP: access control allow origin headers missing
- problems with dart-server/angulardart and CORS
- CORS with Dart, how do I get it to work?

Community
  • 1
  • 1
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • Thanks, but I've already done that. As I said the first HttpRequest.request statement works, but not when using core-ajax-dart. There's either something wrong with the Polymer Dart version of core-ajax called core-ajax-dart, or I've missed something when trying to use core-ajax-dart. I'll add the relevant server side code to my question, for clarification purposes. – James Hurford Aug 26 '14 at 21:12
  • I understand. You could look for bugs in the JavaScript core-ajax-dart repo (could be already closed) because Dart version is a bit behind. – Günter Zöchbauer Aug 26 '14 at 21:14
  • I'll do that, thanks for the suggestion. I don't think it's related to the JavaScript version though, since this is one of the few core_elements that is a pure Dart port. Then it could still be related. I'll look at the JavaScript version and see if I can find a bug relating to this. So what you're saying is I'm actually using core-ajax-dart correctly? – James Hurford Aug 26 '14 at 21:20
  • I think to remember that there were such issues but I have no idea how long ago and if they really were similar but I think it's worth a try. – Günter Zöchbauer Aug 26 '14 at 21:21
  • 1
    I wasn't able to find anything. – Günter Zöchbauer Aug 26 '14 at 21:30