2016-11-17 2 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'がhaのないループ私はそれを知っているが、私はそれがユーザーの選択 –

+0

というプロパティVEの! – 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

上の可変ベースである必要があり、それはどうもありがとうございました 'word' – 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

私が修正を考えていない配列です。 –

関連する問題