无法在ajax中获得任何响应

时间:2017-11-22 15:32:44

标签: jquery html asp.net ajax asp.net-web-api

这是我正在使用的ajax功能。但我仍然没有回应。 我已成功发出警报alert('hi'),但它无效。但是错误的是,警告未定义。

function BindUsers() {

    $.ajax({
        type: 'POST', // Method type
        url: 'https://newsapi.org/v1/articles?source=abc-news-au&sortBy=top&apiKey=1f9da58adc9f4197bc26b657821e0722',
        data: {},
        contentType: 'application/json',
        dataType: 'json',
        success: function (data) {
            alert('hi');
            if (data.d.length > 0) {

            }
        },
        error: function (result) {
            alert(result.d);
        }
    });
}

1 个答案:

答案 0 :(得分:0)

您可以删除正在使用的contentType

 contentType: 'application/json',
在你的ajax调用中

,这样你就不会在控制台上收到以下错误

  

请求标头字段不允许使用Content-Type   预检响应中的Access-Control-Allow-Headers。

这是你的ajax调用它的样子

$.ajax({
    type: 'GET', // Method type
    url: 'https://newsapi.org/v1/articles?source=abc-news-au&sortBy=top&apiKey=1f9da58adc9f4197bc26b657821e0722',
    dataType: 'json',
    success: function (data) {
        alert('hi');
        if (data.d.length > 0) {

        }
    },
    error: function (result) {
        alert(result.d);
    }
});

您正在点击的网址是返回以下回复,并且其中没有任何内容,因此很明显,它会在行if (data.d.length > 0) {上向if (data.articles.length > 0) {

引发错误
{
    "status": "ok",
    "source": "abc-news-au",
    "sortBy": "top",
    "articles": [{
            "author": null,
            "title": "'Butcher of Bosnia' Ratoko Mladic found guilty of genocide",
            "description": "A UN court sentences Bosnian Serb military commander Ratoko Mladic to life in prison for war crimes including genocide and crimes against humanity.",
            "url": "http://www.abc.net.au/news/2017-11-22/ratoko-mladic-found-guilty-of-genocide/9181354",
            "urlToImage": "http://www.abc.net.au/news/image/9168162-1x1-700x700.jpg",
            "publishedAt": "2017-11-22T11:14:23Z"
        },
        {
            "author": "http://www.abc.net.au/news/stephen-dziedzic/166934",
            "title": "The foreign policy White Paper explained",
            "description": "The Federal Government is releasing its foreign policy White Paper today. We break down what's in the document designed to shape Australia's approach to the world for years to come.",
            "url": "http://www.abc.net.au/news/2017-11-23/foreign-policy-white-paper-what-does-it-mean-for-australia/9182188",
            "urlToImage": "http://www.abc.net.au/news/image/9182198-1x1-700x700.jpg",
            "publishedAt": "2017-11-22T14:22:10Z"
        },
        {
            "author": null,
            "title": "Victoria set to make history as voluntary assisted dying bill passes Upper House",
            "description": "Historic voluntary euthanasia laws pass Victoria's Upper House after another marathon sitting.",
            "url": "http://www.abc.net.au/news/2017-11-22/euthanasia-victorian-parliament-passes-assisted-dying-laws/9156016",
            "urlToImage": "http://www.abc.net.au/news/image/9156388-1x1-700x700.jpg",
            "publishedAt": "2017-11-22T05:35:23Z"
        },
        {
            "author": "http://www.abc.net.au/news/rural/lucy-barbour/4592772, http://www.abc.net.au/news/4787636",
            "title": "'It's a fine line': Rinehart's sway over agriculture policy questioned after $40,000 Joyce award",
            "description": "Labor's agriculture spokesman raises concern over the undue influence Gina Rinehart has over Barnaby Joyce.",
            "url": "http://www.abc.net.au/news/2017-11-22/gina-rinehart-influence-over-barnaby-joyce-ag-policy-questioned/9181448",
            "urlToImage": "http://www.abc.net.au/news/image/9178976-1x1-700x700.jpg",
            "publishedAt": "2017-11-22T08:58:18Z"
        },
        {
            "author": "http://www.abc.net.au/news/irena-ceranic/4982426",
            "title": "Olivia Mead's battle for a slice of a 'colossal' mining fortune",
            "description": "As the secret daughter of one of Australia's richest men, few people knew of Olivia Mead's existence.",
            "url": "http://www.abc.net.au/news/2017-11-22/olivia-mead-the-secret-daughter-in-michael-wrights-will/8655548",
            "urlToImage": "http://www.abc.net.au/news/image/9179590-1x1-700x700.jpg",
            "publishedAt": "2017-11-22T06:33:33Z"
        },
        {
            "author": "http://www.abc.net.au/news/avani-dias/7304352",
            "title": "Australians of the Year unite in support of medical aid in Manus centre",
            "description": "Twelve former Australians of the Year have written an open letter pleading with the Prime Minister to allow doctors inside the recently closed Manus Island detention centre.",
            "url": "http://www.abc.net.au/news/2017-11-23/australians-of-the-year-open-letter-to-pm-on-manus-island/9181874",
            "urlToImage": "http://www.abc.net.au/news/image/9182026-1x1-700x700.jpg",
            "publishedAt": "2017-11-22T13:18:02Z"
        },
        {
            "author": null,
            "title": "Kerr's strike helps secure Matildas' 3-0 win over China",
            "description": "The Matildas win their sixth straight match, with Sam Kerr scoring a double in a 3-0 defeat of China.",
            "url": "http://www.abc.net.au/news/2017-11-23/sam-kerr-propels-matildas-to-victory-against-china/9181964",
            "urlToImage": "http://www.abc.net.au/news/image/9182168-1x1-700x700.jpg",
            "publishedAt": "2017-11-22T13:21:33Z"
        },
        {
            "author": null,
            "title": "Parents blame Government for 'toxic river' making kids sick",
            "description": "Outback parents were forced to drive their children hundreds of kilometres to bathe after a “man-made drought” exposed them to toxic river water.",
            "url": "http://www.abc.net.au/news/2017-11-22/parents-blame-government-for-toxic-river-making-kids-sick/9181886",
            "urlToImage": "http://www.abc.net.au/news/image/9181842-1x1-700x700.jpg",
            "publishedAt": "2017-11-22T12:00:00Z"
        },
        {
            "author": "http://www.abc.net.au/news/dan-colasimone/6476504",
            "title": "How it all unravelled for Ange Postecoglou",
            "description": "Ange Postecoglou actually did it — he quit the Socceroos gig on the eve of the World Cup. What would motivate a man to exit stage left right before the big show?",
            "url": "http://www.abc.net.au/news/2017-11-22/how-it-all-unravelled-for-ange-postecoglou/9179040",
            "urlToImage": "http://www.abc.net.au/news/image/8617900-1x1-700x700.jpg",
            "publishedAt": "2017-11-22T02:20:59Z"
        },
        {
            "author": "http://www.abc.net.au/news/michael-collett/166910, http://www.abc.net.au/news/james-maasdorp/167000",
            "title": "Bring back Aussie Guus: Your verdict on who should be the next Socceroos coach",
            "description": "Ange Postecoglou won't be taking Australia to Russia 2018. Here's who you suggested should replace him as Socceroos coach.",
            "url": "http://www.abc.net.au/news/2017-11-22/who-should-replace-ange-postecoglou-as-socceroos-coach/9180512",
            "urlToImage": "http://www.abc.net.au/news/image/9016252-1x1-700x700.jpg",
            "publishedAt": "2017-11-22T09:18:22Z"
        }
    ]
}