Troubleshooting Ecom Express API Integration for AWB Number Retrieval

I’m having trouble getting the Ecom Express API to work in my project. I can get a JSON response when I use Postman to fetch an AWB Number. But when I try to do the same thing in my code, I’m not getting any response. Here’s what I’ve tried:

<form id="shipmentForm" name="shipmentForm" enctype="multipart/form-data">
    <input type="text" id="login" name="login" value="testuser" />
    <input type="text" id="pass" name="pass" value="S3cur3P@ssw0rd" />
    <input type="text" id="quantity" name="quantity" value="3" />
    <input type="text" id="mode" name="mode" value="prepaid" />
</form>

<script>
function fetchTrackingNumber() {
    var formInfo = new FormData(document.getElementById('shipmentForm'));
    $.ajax({
        url: 'https://api.courier-service.com/v2/get_tracking/',
        method: '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);
        }
    });
}
</script>

I’m working with Razor v3. Any ideas on what I might be doing wrong or how to debug this? Thanks for any help!

Hey Sophie26, sounds like you’re in a bit of a pickle with that Ecom Express API! I’ve been there before, and it can be super frustrating. Have you considered that it might be a CORS issue? Sometimes browsers block requests to different domains for security reasons.

Maybe try adding some headers to your AJAX call? Something like this:

$.ajax({
    // ... your existing options ...
    headers: {
        'Access-Control-Allow-Origin': '*',
        'Content-Type': 'application/json'
    },
    // ... rest of your options ...
});

Also, are you sure the API endpoint is expecting form data? Some APIs prefer JSON. You might want to try stringifying your data instead:

data: JSON.stringify(Object.fromEntries(formInfo)),
contentType: 'application/json',

Oh, and have you checked your browser’s console for any error messages? Sometimes there are helpful clues hiding in there!

Let us know if any of that helps. If not, maybe you could share a bit more about what you’re seeing (or not seeing) in the response? Debugging APIs can be like solving a mystery sometimes!

Hey sophie26, have u tried using fetch instead of ajax? sometimes it works better wit modern apis. also, double-check ur api key and make sure its valid. heres a quick example:

fetch('https://api.courier-service.com/v2/get_tracking/', {
  method: 'POST',
  body: formInfo
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));

hope this helps! lemme know if u need more info

In my experience, similar issues often stem from subtle mismatches between the API documentation and the implementation. Make sure the endpoint URL is exactly as specified in the documentation and that your login credentials are valid for the environment you are targeting. It is also important to include any necessary headers for the request and to verify that your server settings allow for cross-origin requests. Using browser developer tools or software like Fiddler can help identify discrepancies between the requests you make in code and those sent by tools like Postman.