2

So I'm working on Bigcommerce to build a website for a client. since Bigcommerce does not allow the upload of PHP files on their system I'm using AJAX in a script tag in an HTML file. The documentation for the API I'm using says to use my API key in the username field for HTTP Basic Authentication, and that all requests must be transmitted over HTTPS.

So firstly, here's my code

$.ajax({
        type: 'POST',
        url : "https://www.freightview.com/api/v1.0/rates",
        data : ({ myRequest }),
        dataType : "jsonp",
        Accept: 'application/json',
        beforeSend : function(xhr) {
        xhr.setRequestHeader("Authorization", "Basic " +           "MyKey:");
        }
})

I've tried doing Username : 'mykey' and some other various ways to put the key in there but keep getting 401 Unauthroized

I've searched around for some ways to do this and noted a few possible issues:

  • Ajax GET request over HTTPS It's mentioned on this page that AJAX cannot make cross-domain HTTPS requests. Does this mean I have to find another way to make the request? If so, how can I make such a request with the limitations I have?
  • Is it simply that I'm not properly defining my username for the Authentication? I'm hoping this is the only problem, but I've looked at a lot of questions on this site about Basic Auth in AJAX.
  • JQuery Ajax calls with HTTP Basic Authentication The user who asks this question seems to have a similar problem to mine, but the answers are quite varied, and I'm not sure if they'll work in my situation or even how to begin implementing them. Do I need CORS? Can I even get CORS to work on Bigcommerce?

So as you can see what I thought would be a relatively simple snippet of code turned out to be a lot more complex than I thought, and I could really use some of that StackOverflow magic!

Edit: grammar and spelling

Community
  • 1
  • 1
Csteele5
  • 1,262
  • 1
  • 18
  • 36
  • 1
    Does console mention a CORS warning? I feel if you are receiving the 401 response, then your request is going through. I use proxied ajax GET requests for Bigcommerce to bypass CORS. –  Jul 08 '15 at 05:52

1 Answers1

1

In BigCommerce you have to encode your credentials with Base64.

Example:

// Define the string
var string = 'Hello World!';

// Encode the String
var encodedString = btoa(string);
console.log(encodedString); // Outputs: "SGVsbG8gV29ybGQh"

// Decode the String
var decodedString = atob(encodedString);
console.log(decodedString); // Outputs: "Hello World!"
Zeezos Inc
  • 11
  • 1