我是JavaScript新手,正在寻求帮助。我正在尝试根据文本框输入发出JSON请求。我希望框中的文本成为请求的一部分,但是我不能使文本成为字符串,以便它可以用于JSON请求。我做错了什么?
这是我的代码:
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>
<body>
<p id="syno"></p>
<form name="test">
<input type="text" name="Edit"> <!-- where i input a word-->
<input type="button" value="Test" onClick="gettext()"><!-- test button-->
<input type="text" name="Edit2" value="power" readonly style="border:1px solid white">
</form>
<script>
function gettext() {
document.test.Edit2.value = document.test.Edit.value;
document.test.Edit2("Edit2").value = document.test.Edit.value;
}
var apiKey = '0771a4c95ca7e23d41e33f67a1da0000/';
var txtjson = '/json';
var f = document.test.Edit2.value;
var apiUrl = 'http://words.bighugelabs.com/api/2/' + apiKey + f + txtjson;
$.ajax({
url: apiUrl,
type: "GET",
dataType: 'json',
success: parseWord
});
function parseWord(data) {
$('#syno').text(data.noun.syn);
}
</script>
</body>
</html>
答案 0 :(得分:1)
代码中的问题
ajax
代码但代码超出了gettext()
功能document.test.Edit2("Edit2")
应为document.forms.test.Edit2.value
查看正确的代码,
function gettext() {
document.forms.test.Edit2.value = document.forms.test.Edit.value;
var apiKey = '0771a4c95ca7e23d41e33f67a1da0000/';
var txtjson = '/json';
var f = document.forms.test.Edit2.value;
var apiUrl = 'http://words.bighugelabs.com/api/2/' + apiKey + f + txtjson;
$.ajax({
url: apiUrl,
type: "GET",
dataType: 'json',
success: parseWord
});
return false;
}
function parseWord(data) {
$('#syno').text(data.noun.syn);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p id="syno"></p>
<form name="test">
<input type="text" name="Edit" />
<!-- where i input a word-->
<input type="button" value="Test" onClick="return gettext(this);" />
<!-- test button-->
<input type="text" name="Edit2" value="power" readonly style="border:1px solid white" />
</form>