title: Access to REST API from JS
body: <p>Need access to REST API from JS code, using jQuery ajax:</p>
  
  <pre><code>function tryQwintry () {
      var data = {
          "params[weight]" : "100",
          "params[dimensions]" : "100x100x100",
          "params[delivery_pickup]" : "msk_1",
          "params[insurance]" : "false",
          "params[items_value]" : "350",
          "params[retail_pricing]" : "1"
      };
  
      $.ajax({
          url: "http://logistics.qwintry.com/api/cost",
          type: "POST",
          dataType: "jsonp",
          contentType: "application/json",
          headers: {"Authorization":"Bearer " + MY_API_KEY},
          data: data,
          success: function (cost) {
              console.log("стоимость доставки $"+cost);
          },
          error: getErrorMsg
      });
  }
  </code></pre>
  
  <p>Documentation of API (all examples are PHP):</p>
  
  <pre><code>&lt;?php
      define('SITE_URL', 'logistics.qwintry.com');
      define('API_KEY', 'YOUR_API_KEY'); //don't forget to set your key!
  
      $url =  'http://'. SITE_URL .'/api/cost';
  
  
      $data = array ( 
          'params' =&gt; array(
              'weight' =&gt; 5, // in lb
              'delivery_pickup' =&gt; 'msk_1', // full list of pickup points can be retrieved from /api/locations-list
              'insurance' =&gt; true,
              'items_value' =&gt; 500, // declaration items total cost in USD
              'retail_pricing' =&gt; true // retail / wholesale pricing?
          ),
       );
      $data_string = http_build_query($data);
  
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer '. API_KEY));
      curl_setopt($ch, CURLOPT_POST, true);
      curl_setopt($ch, CURLOPT_POSTFIELDS,  $data_string);
      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
      $response = curl_exec($ch);
      curl_close($ch);
      var_dump($response);
  </code></pre>
  
  <p>Same thing I've coded in Java:</p>
  
  <pre><code>public double getCostPickup(String weight, String dimensions, String toPickup, String insurance, String value) throws Exception {
  
      Map&lt;String, Object&gt; params = new HashMap&lt;&gt;();
      params.put("params[weight_kg]", weight);
      params.put("params[dimensions_cm]", dimensions);
      params.put("params[delivery_pickup]", toPickup);
      params.put("params[insurance]", insurance);
      params.put("params[items_value]", value);
      params.put("params[retail_pricing]", RETAIL_PRICING);
  
      String url = BASE_URL+"/api/cost";
      HttpResponse&lt;JsonNode&gt; jsonResponse = Unirest.post(url).fields(params).asJson();
      return getCost(jsonResponse, insurance);
  }
  </code></pre>
  
  <p>Have problems with configuring data of ajax request.
  So any help would be greatly appreciated.</p>
  
  <p><strong>UPDATE:</strong> Changed my JS code:</p>
  
  <pre><code>function tryQwintry () {
      var data = {
          "params[weight]" : "100",
          "params[dimensions]" : "100x100x100",
          "params[delivery_pickup]" : "msk_1",
          "params[insurance]" : "false",
          "params[items_value]" : "350",
          "params[retail_pricing]" : "1"
      };
  
      $.ajax({
          url: "http://logistics.qwintry.com/api/cost",
          type: "POST",
          dataType: "json",
          contentType: "application/json",
          headers: {"Authorization" : "Bearer"+MY_API_KEY, "Access-Control-Allow-Origin" : "true"},
          data: JSON.stringify(data),
          success: function (cost) {
              console.log("стоимость доставки $"+cost);
          },
          error: getErrorMsg
      });
  }
  </code></pre>
  
  <p>Getting this error in Chrome's developers mode:
  <a href="https://i.stack.imgur.com/7geZg.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7geZg.png" alt="enter image description here"></a></p>