jQuery Ajax请求错误函数问题

时间:2015-03-24 13:45:13

标签: ajax function

我正在制作完全正常的Ajax请求。我遇到的问题是我将“错误”方法附加到调用上,当我修改URL以尝试使其抛出错误时......我没有得到任何错误。这是我的代码:

function instagramAjax () {
		$.ajax ({
			url: "url that works",
			dataType: "jsonp",
			success: function(returnedData) {
				//my success code that works
			},//end success
			error: function(jqXHR, textStatus, errorThrown) {
				console.log("Error " + errorThrown);
			}
		});//end ajax request
	}//end instagramAjax function

1 个答案:

答案 0 :(得分:0)

您设置的网址是什么? 不会为跨域脚本和跨域JSONP请求调用此处理程序。

  window.MyCallback = function (data) {
       console.log(data);
    };

    function instagramAjax () {
            $.ajax ({
                url: "url that works",
                dataType: "jsonp",
                crossDomain: true,
                jsonpCallback: 'MyCallback',
                success: function(returnedData) {
                    //my success code that works
                },//end success
                error: function (xhr, status, err) {
                    console.log(status, err);
                }
            });//end ajax request
        }//end instagramAjax function

或者你可以这样做:

function instagramAjax () {
            $.ajax ({
                url: "url that works",
                dataType: "jsonp",
                crossDomain: true,
                jsonp: 'MyCallback',
                success: function(returnedData) {
                    //my success code that works
                },//end success
                error: function (xhr, status, err) {
                    console.log(status, err);
                }
            });//end ajax request
        }//end instagramAjax function