Javascript absolute path to URL works on one page,

2019-07-12 08:49发布

问题:

I have two URLs:

http://s140452.gridserver.com/view-pullsheet/

https://s140452.gridserver.com/locations/

When browsing through any properties on the locations page (as well as any single property, filtering properties using the sidebar, adding and removing properties from the "pullsheet") the jcart.js script works fine -- it is in control of adding and removing photos from the "pullsheet".

From this page however, http://s140452.gridserver.com/view-pullsheet/ , I get the following error "Ajax error: Edit the path in jcart.js to fix".

The code in question is:

    var path = 'https://s140452.gridserver.com/wp-content/themes/re/assets/functions/jcart',
        container = $('#jcart'),
        token = $('[name=jcartToken]').val(),
        tip = $('#jcart-tooltip');

    var config = (function() {
        var config = null;
        $.ajax({
            url: path + '/config-loader.php',
            data: {
                "ajax": "true"
            },
            dataType: 'json',
            async: false,
            success: function(response) {
                config = response;
            },
            error: function() {
                alert('Ajax error: Edit the path in jcart.js to fix.');
            }
        });
        return config;
    }());

How can the script be working fine on the locations page but throw an error from a different URL? How can I fix this?

Thanks!

jcart.js is located @ https://s140452.gridserver.com/wp-content/themes/re/assets/js/jcart.js

回答1:

because you are running into the same origin problem. https vs http

var path = 'https://s140452.gridserver.com/wp-content/themes/re/assets/functions/jcart',

to

var path = '//s140452.gridserver.com/wp-content/themes/re/assets/functions/jcart',


回答2:

With your error function you are assuming that the error can only be the incorrect path. Why not implement the function as error(jqXHR, textStatus, errorThrown) (see ajax), put a breakpoint in that function and then find out the true error.