cancel
Showing results for 
Search instead for 
Did you mean: 

Getting 404 when calling my custom REST from jQuery but accessing the url in browser works very OK

Getting 404 when calling my custom REST from jQuery but accessing the url in browser works very OK

Guys, Need your help regarding my issue.  

 

I can access my REST url directly through the browswer with out issues and I can see the JSON result.  

But when using Jquery, I am getting a 404.

 

<script type="text/javascript"><!--
require(["jquery"], function($) {
    function getCategories(bid, bclass) {
        jQuery(bclass + " select").empty();
        var soapUrl = location.protocol + '//' + location.host + '/rest/V1/rahahahaapi/getCategories/' + bid;
        jQuery.ajax({
            url: soapUrl,
            dataType: 'json',
            success: function(json) {
                if (!json.Error) {
                    x = json.Result.categories;
                    jQuery.each(x, function(key, value) {
                        var opt = jQuery("<option></option>").data("category_depth", value.category_depth).data("category_is_last", value.category_is_last).val(value.category_id).text(value.category_name);
                        jQuery(bclass + " select").append(opt);
                    });
                    jQuery(bclass + " select").fadeIn("fast");
                } else {
                    alert("API error encountered.");
                }
            }
        });
    }
    $(document).ready(function() {
        getCategories(0, "#mainCat");
    });
});
</script>

that code I put inside customtab.phtml.  basically the module I made addes a new tab and a couple of dropdowns into the Product New/Edit form.  On document ready, jquery calls on the my REST api, to retreive client specific categories.

 

this is the function that is being called:

public function getCategories($id = 0) {
        $categories = $this->clsRah->getCategories($id);
        
        echo json_encode($categories);
        die();
    }

This is the console log:

Failed to load resource: the server responded with a status of 404 (Not Found)

 

Sometimes it works but majority, I get the 404.  I am not sure what is causing this.

1 REPLY 1

Re: Getting 404 when calling my custom REST from jQuery but accessing the url in browser works very

Update as of today, April 14, 2016.  This is now working, but I am not sure how it got working.