开发者

Why does my jquery.ajax() not fire success or error?

I have been staring at this for 6 hours. I don't understand.

$.ajax({
    url: "http://www.band.dev:8888/datafeeder/hello_world",
    success: function( data ) {
        alert (data);
    },
    error: function(request, statu开发者_StackOverflow社区s, error) {
        alert(status + ' - ' + error);
    }
});

I'm running MAMP locally, and when I hit the URL directly it echos 'hello world', no problem. When I run this, I get a dialog box with 'error - '. I've tried adding dataType:'html' in there, no help. Thoughts?

-- EDIT --

So this is my ACTUAL problem. When I run this, neither success nor error are fired, and I can see that the JSON is correct when I hit the URL directly. (BTW, the relative url fix worked for the above bit of code.)

$( "#member_type" ).autocomplete({
        source: function( request, response ) {
            $.ajax({
                url: "/datafeeder/get_member_types/json",
                dataType: "jsonp",
                data: {
                    //maxNum: 12,
                    searchString: request.term
                },
                search: function(event, ui) {
                    alert('searching');
                },
                success: function( data ) {
                    alert (data);
                    response( $.map( data, function( item ) {
                        return {
                            label: item.type,
                            value: item.id
                        }
                    }));
                },
                error: function( request, status, error) {
                    alert (status + ' - ' + error);
                }
            });
        }
});


You are probably hitting the same origin policy restriction which forbids you from sending AJAX requests to different domains than the one hosting your script. The best way to ensure that you are not violating this policy is to use relative urls for your AJAX calls:

$.ajax({
    url: "/datafeeder/hello_world",
    success: function( data ) {
        alert (data);
    },
    error: function(request, status, error) {
        alert(status + ' - ' + error);
    }
});

For this AJAX request to work, the HTML page hosting the this javascript must be hosted on http://www.band.dev:8888.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜