I have been working with a Shopify development store for the past year, and noticed that the protocol for making successful POST requests, via Ajax calls, have definitely changed. I have been only able to make successful GET requests, however every POST request results in a 400 Bad Request error.
Interestingly enough, these post requests work on my local host and on my ngrok url. It is only when I’m sending the post request to my store domain where specifically POST requests result in the 400 error.
I am leveraging .NET razor pages and treat each page as the front and back end of an app proxy. I am willing to leverage webhooks or theme extensions but after am month of looking at the docs and other forums, I’m primarily looking for a certain solution to make HTTP POST calls in shopify (not using the shopify API but sending the data to an external API)
So the following ajax call works only on my localhost and ngrok url. I extract inputted data from a form when a user hits the submit button:
var formData = {
CollectorId: $('#CollectorId').val(),
FirstName: $('#FirstName').val(),
LastName: $('#LastName').val(),
EmailAddress: $('#EmailAddress').val(),
PhoneNumber: $('#PhoneNumber').val(),
Address1: $('#address1').val(),
Address2: $('#Address2').val(),
City: $('#city').val(),
StateRegionProvince: $('#stateRegionProvince').val(),
PostalCode: $('#postalCode').val()
};
and attempt the following ajax setup to my app proxy’s url and specifying the handler method in my razor backend file:
$.ajax({
type: 'POST',
url: '/apps/member?handler=HandlerMethodName',
contentType: 'application/json',
data: JSON.stringify(formData),
headers: {
'Content-Type': 'application/json'
},
dataType: 'json',
beforeSend: function (jqXHR, settings) {
console.log('Request URL:', settings.url);
console.log('Request Type:', settings.type);
},
xhrFields: {
withCredentials: true
},
success: function (response) {
if (response.success) {
$('#UserFeedbackMessage').text(response.message).css('color', 'green').show();
} else {
$('#UserFeedbackMessage').text(response.message).css('color', 'red').show();
}
},
error: function (xhr, status, error) {
console.error("Error updating collector info:", error);
console.log("XHR:", xhr);
console.log("Status:", status);
console.log("Response Text:", xhr.responseText);
$('#UserFeedbackMessage').text("An error occurred while updating your profile.").css('color', 'red').show();
},
complete: function (jqXHR, status) {
console.log('Complete Response Headers:', jqXHR.getAllResponseHeaders());
}
});