Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

400 Error when validating address with AJAX #14

Open
britteninc opened this issue Jan 13, 2020 · 3 comments
Open

400 Error when validating address with AJAX #14

britteninc opened this issue Jan 13, 2020 · 3 comments

Comments

@britteninc
Copy link

britteninc commented Jan 13, 2020

I am getting the following error when trying to validate an address with the AJAX sample code provided in the documentation: Failed to load resource: the server responded with a status of 400 (Bad Request). /actions/avatax/json/validate-address

I believe the issue is the "url" setting in the ajax code; I do not know where to point this or if it needs to remain the same.

Here is the js code being used (provided from the documentation):

$('#address-form').on('submit.addressValidation', function(e) {

    e.preventDefault();

    var $form = $(this);

    var data = {
        address1   : $('[name="shippingAddress[address1]"]').val(),
        address2   : $('[name="shippingAddress[address2]"]').val(),
        city       : $('[name="shippingAddress[city]"]').val(),
        zipCode    : $('[name="shippingAddress[zipCode]"]').val(),
        stateValue : $('[name="shippingAddress[stateValue]"]').val(),
        countryId  : $('[name="shippingAddress[countryId]"]').val()
    };

    var csrfTokenName  = "{{ craft.app.config.general.csrfTokenName }}";
    var csrfTokenValue = "{{ craft.app.request.csrfToken }}";

    data[csrfTokenName] = csrfTokenValue;

    $.ajax({
        type: 'post',
        url: '/actions/avatax/json/validate-address',
        data: data,
        dataType: 'json'
    }).done(function(data){

        console.log(data);

        if(data.success) {
            // valid address
            $form.off('submit.addressValidation').submit();
        } else {

            // handle error here...

            return false;
        }
    });
});
@imagehat
Copy link
Collaborator

@jlapan - Sorry for the delay, did you ever get this sorted? The json validation is a feature we added but haven't really used on a site to date so we haven't needed to troubleshoot that very much, but if not here are a few thoughts.

If it's the server response straight from Craft then I don't think it's the url—that url in the example is the Craft route to the json controller's validateAddress() function so it should be right—it might be that the csrf token is not being set correctly.

But I did recently realize that the Avalara API will return a 400 error message in the response if the address is not sent correctly. That seems to happen if the address fields are not sent correctly, e.g. make sure the page you're trying this on has the inputs named "shippingAddress[address1]" or they may need to change to "address[address1]" depending on the edit address form vs. the checkout form in the example.

Lastly, there was an error that could be caused if the address model wasn't populated correctly which has been fixed in v2.0.8.

@britteninc
Copy link
Author

@imagehat I did not. I currently have it disabled until I can get sandbox access to the client's Avalara account to further test and diagnose the issue.

@imagehat
Copy link
Collaborator

Ok. When you get back to it just ping me or Jason know again if you run into issues and we can try to help figure it out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant