Integration of Ecom Express API: Help Needed

I’m stuck trying to get Ecom Express APIs working in my project. Postman gives me the right JSON output when I try to get an AWB Number. But my code isn’t getting any response.

Here’s what I’ve got so far:

<form id="shipForm" name="shipForm" enctype="multipart/form-data">
  <input type="text" id="account" name="account" value="ecomtest" />
  <input type="text" id="key" name="key" value="Sh1p@3xP" />
  <input type="text" id="quantity" name="quantity" value="3" />
  <input type="text" id="mode" name="mode" value="prepaid" />
</form>

<script>
function fetchAWBNumber() {
  var formInfo = new FormData($('#shipForm')[0]);
  $.ajax({
    url: 'http://test.ecomexpress.com/api/fetch_awb/',
    type: 'POST',
    data: formInfo,
    dataType: 'json',
    cache: true,
    contentType: "multipart/form-data; boundary=----FormBoundaryXYZ123",
    processData: false,
    success: function (response) {
      console.log(response);
    }
  });
}
</script>

I’m using Razor v3. Any ideas what I’m doing wrong? Thanks!

Hey Grace_42Read! I’ve been tinkering with Ecom Express APIs too, and I totally get your frustration. Have you considered using the Fetch API instead of jQuery AJAX? It might give you more control over the request.

Here’s a quick example of how you could modify your code:

function fetchAWBNumber() {
  const form = document.getElementById('shipForm');
  const formData = new FormData(form);

  fetch('http://test.ecomexpress.com/api/fetch_awb/', {
    method: 'POST',
    body: formData
  })
  .then(response => response.json())
  .then(data => console.log(data))
  .catch(error => console.error('Error:', error));
}

Also, are you sure the API endpoint is correct? Sometimes test environments have different URLs or require additional headers. Have you checked their documentation for any specific requirements?

By the way, how are you triggering the fetchAWBNumber function? Is it tied to a button click or form submission? Just curious about your setup! Let me know if this helps or if you need more ideas to troubleshoot.

hey grace, i’ve dealt with ecom express before. make sure ur using https instead of http in the url. also, try adding headers like this:

headers: {
‘Accept’: ‘application/json’,
‘Authorization’: 'Bearer ’ + yourApiKey
}

if that doesnt work, double check ur api key. sometimes they expire n u gotta get a new 1. good luck!

I had a similar issue when integrating Ecom Express API. The problem might be in your content type and how you’re handling the form data. Try changing your AJAX call like this:

$.ajax({
    url: 'http://test.ecomexpress.com/api/fetch_awb/',
    type: 'POST',
    data: formInfo,
    dataType: 'json',
    cache: false,
    contentType: false,
    processData: false,
    success: function (response) {
        console.log(response);
    },
    error: function(xhr, status, error) {
        console.error('Error:', error);
    }
});

Remove the contentType line and set cache to false. Also, add an error callback to see if there are any issues. If this doesn’t work, check your browser’s network tab for any CORS errors. You might need to add appropriate headers or use a proxy if it’s a cross-origin issue.