2017-05-19 1 views
-2
$.ajax({ 
    url: "https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro=&explaintext=&titles=india", 
    data: queryData, 
    dataType: 'json', 
    type: 'GET', 
    headers: { 'Api-User-Agent': 'Example/1.0' }, 
    success: function(data) { 
     console.log("success"); 
    }, 
    error: function(){ 
     console.log("error"); 
    } 
}); 

항상 오류이 표시됩니다. 그러나 일반적으로 브라우저에 해당 URL을 붙여 넣을 때 적절한 응답을받습니다.왜 AJAX 통화가 진행되지 않습니까?

+0

오류 무엇입니까? – punkbit

+0

나는 오류 부분이 성공한 부분이 아니라 실행되고 있다는 것을 의미한다. –

+0

기본적으로 자신이 아닌 다른 도메인으로 Ajax 요청을 실행하는 것은 허용되지 않습니다. 보안상의 이유로 – OptimusCrime

답변

-1

시도해 볼 수 있습니까? 나는 당신에게

var settings = { 
    "async": true, 
    "crossDomain": true, 
    "url": "https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro=&explaintext=&titles=india", 
    "method": "POST", 
    "headers": { 
    "content-type": "application/json", 
    "cache-control": "no-cache" 
    } 
} 

$.ajax(settings).done(function (response) { 
    console.log(response); 
}); 

응답 도움이 될 수 있다고 생각 :

{ 
    "batchcomplete": "", 
    "query": { 
    "normalized": [ 
     { 
     "from": "india", 
     "to": "India" 
     } 
    ], 
    "pages": { 
     "14533": { 
     "pageid": 14533, 
     "ns": 0, 
     "title": "India", 
     "extract": "India, officially the Republic of India (Bhārat Gaṇarājya), is a country in South Asia. It is the seventh-largest country by area, the second-most populous country (with over 1.2 billion people), and the most populous democracy in the world. It is bounded by the Indian Ocean on the south, the Arabian Sea on the southwest, and the Bay of Bengal on the southeast. It shares land borders with Pakistan to the west; China, Nepal, and Bhutan to the northeast; and Myanmar (Burma) and Bangladesh to the east. In the Indian Ocean, India is in the vicinity of Sri Lanka and the Maldives. India's Andaman and Nicobar Islands share a maritime border with Thailand and Indonesia.\nThe Indian subcontinent was home to the urban Indus Valley Civilisation of the 3rd millennium BCE. In the following millennium, the oldest scriptures associated with Hinduism began to be composed. Social stratification, based on caste, emerged in the first millennium BCE, and Buddhism and Jainism arose. Early political consolidations took place under the Maurya and Gupta empires; the later peninsular Middle Kingdoms influenced cultures as far as southeast Asia. In the medieval era, Judaism, Zoroastrianism, Christianity, and Islam arrived, and Sikhism emerged, all adding to the region's diverse culture. Much of the north fell to the Delhi sultanate; the south was united under the Vijayanagara Empire. The economy expanded in the 17th century in the Mughal Empire. In the mid-18th century, the subcontinent came under British East India Company rule, and in the mid-19th under British crown rule. A nationalist movement emerged in the late 19th century, which later, under Mahatma Gandhi, was noted for nonviolent resistance and led to India's independence in 1947.\nIn 2015, the Indian economy was the world's seventh largest by nominal GDP and third largest by purchasing power parity. Following market-based economic reforms in 1991, India became one of the fastest-growing major economies and is considered a newly industrialised country. However, it continues to face the challenges of poverty, corruption, malnutrition, and inadequate public healthcare. A nuclear weapons state and regional power, it has the third largest standing army in the world and ranks fifth in military expenditure among nations. India is a federal republic governed under a parliamentary system and consists of 29 states and 7 union territories. It is a pluralistic, multilingual and multi-ethnic society and is also home to a diversity of wildlife in a variety of protected habitats." 
     } 
    } 
    } 
} 

또는

$.ajax({ 
    "async": true, 
    "crossDomain": true, 
    url: "https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro=&explaintext=&titles=india", 
    dataType: 'json', 
    type: 'POST', 
    headers: { 
     "content-type": "application/json", 
     "cache-control": "no-cache" 
    }, 
    success: function(data) { 
     console.log("success"); 
    }, 
    error: function(){ 
     console.log("error"); 
    } 
}); 
+0

첫 번째 방법에서 오류가 발생했습니다 :'No 'Access-Control-Allow-Origin'헤더가 요청 된 리소스에 있습니다. ' 두 번째 방법에서 오류가 발생합니다 :'Uncaught ReferenceError : queryData is not defined ' –

+0

파일은 .php입니까? PHP 코드 -> 헤더 ('Access-Control-Allow-Origin : *');를 입력 해보십시오. 두 번째 방법에서는 오류가 발생합니다. 정상적인 이유는 데이터를 보내는 것입니까? 위키 백과는 URL에서 데이터를 얻을 수 있습니까? 데이터없이 헤더 ('Access-Control-Allow-Origin : *')로 시도하십시오 –

+0

나는 콘솔에 액세스 할 수없는 codepen.io에서 시도하고 있습니다. 로컬 일을하는 동안 나는 잡히지 않는 참조 오류가 발생했습니다. 그러나 codepen.io에서 요청이 작동하지 않습니다. –

관련 문제