Javascript absolute path to URL works on one page, but creates ajax error message on another

僤鯓⒐⒋嵵緔 提交于 2019-12-11 02:28:03

问题


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.



来源:https://stackoverflow.com/questions/9449627/javascript-absolute-path-to-url-works-on-one-page-but-creates-ajax-error-messag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!