2016-11-17 1 views
0
function wrong(word){ 

$("#main-display").html(""); 
$("#main-display").append("Sorry that is not a valid word"); 


var word = "lighht"; 
var check = "https://montanaflynn-spellcheck.p.mashape.com/check/?text=" + word; 

// Ajax request to wordsapi 
// Will return the synonyms of the searched word 
$.ajax({ 
    url: check, // The URL to the API. You can get this in the API page of the API you intend to consume 
    type: 'GET', // The HTTP Method, can be GET POST PUT DELETE etc 
    data: {}, // Additional parameters here 
    dataType: 'json', 
    success: function(data) { console.dir((data.source)); console.log(data);}, 
    error: function(err) { wrong(word) }, 
    beforeSend: function(xhr) { 
    console.log(check); 
    xhr.setRequestHeader("X-Mashape-Authorization", "key"); // Enter here your Mashape key 
    } 



}).done(function(response){ 
    console.log(word); 
    console.log(response); 
    var hey = Object.keys(response.corrections); 
    console.log(hey); 
    console.log(response.corrections +".lighht"); 
    for (var i = 0; i < 10; i++) { 
     console.log(Object.keys(response.corrections.lighht)); 
    } 

}); // End of ajax of synonyms 

그래서 사용자가 입력 한 단어에 가능한 기본 단어를 반환하는 함수를 만들려고합니다. 내가 잘 작동하는 API를 찾았지만 화면에 데이터를 가져 오는 데 어려움을 겪고 있습니다. JSON 객체는 다음과 같습니다 :JavaScript를 사용하여 Ajax 요청을하고 있습니다. 객체를 통과하는 데 도움이 필요합니다.

"lighht"의 객체 키는 단어를 기반으로 변경 될 것입니다. 그래서 다음과 같이 변수를 만들려고 할 때 :

{ 
    "original": "lighht", 
    "suggestion": "light", 
    "corrections": { 
    "lighht": [ 
     "light", 
     "sleight", 
     "hightail", 
     "alright", 
     "Bligh", 
     "Lhotse", 
     "Galahad" 
    ] 
    } 
} 

console.log(Object.keys(response.corrections.word[i])); 

작동하지 않고 중단됩니다.

그래서이 데이터를 가져 오는 방법을 알아야합니다. 그냥 $.each()을 사용할 수 있도록 jQuery를 사용하는

+3

는'corrections', 하하지 않는 루프 나는 '단어'라는 속성을 가지고있다. –

+0

나는 그것을 알지 만 사용자 선택에있어 변수가되는 것이 필요하다. – heybit

답변

1

:

var response = { 
 
    "original": "lighht", 
 
    "suggestion": "light", 
 
    "corrections": { 
 
    "lighht": [ 
 
     "light", 
 
     "sleight", 
 
     "hightail", 
 
     "alright", 
 
     "Bligh", 
 
     "Lhotse", 
 
     "Galahad" 
 
    ], 
 
    "cooool": [ 
 
     "cool", 
 
     "car" 
 
    ] 
 
    } 
 
}; 
 

 
// If you want to iterate over all of the corrections: 
 
$.each(response.corrections, function(c) { 
 
    console.log(response.corrections[c]); 
 
}); 
 

 
var userInput = 'cooool'; 
 

 
// Access an individual item: 
 
console.log(response.corrections[userInput]);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

+0

고맙습니다. !!! – heybit

0

사용

var response = { 
 
    "original": "lighht", 
 
    "suggestion": "light", 
 
    "corrections": { 
 
    "lighht": [ 
 
     "light", 
 
     "sleight", 
 
     "hightail", 
 
     "alright", 
 
     "Bligh", 
 
     "Lhotse", 
 
     "Galahad" 
 
    ] 
 
    } 
 
} 
 

 

 
$.each(response.corrections, function(v) { 
 
     console.log(v); 
 
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

+0

수정 사항이 배열이라고 생각하지 않습니다. –

관련 문제