我想GET
用ajax 发出一个API 请求。我有一个 jquery 函数从第一个输入字段中获取一个 api 键值,然后在输入字段 #2 上显示一个连接结果url + api key
。我需要对第二个输入字段中显示的值发出获取请求。我怎样才能完成这个获取请求?目标服务器设置为允许跨域脚本站点
<script>
$(document).ready(function () {
$(document).ready(function() {
/** get the inputs we might need */
var $result = $('#result');
var $input = $('#input');
$result.data('url', $result.val());
var timer;
/** function to submit data to the server and
update the result input on success */
function submitForm( input, newValue) {
$.ajax({
type: "POST",
url: "/concatenate/index.php",
data: {input:input},
success: function (data) {
$result.val(newValue);
}
});
};
/** on key up, fill #result with the url + input */
$input.bind('keyup', function() {
var $this = $(this);
var inp = $this.val();
var url = $result.data('url');
var newValue = url + inp + '/';
if(timer) { clearTimeout(timer); }
timer = setTimeout(function(){
submitForm(inp, newValue) ;
}, 40);
return false;
});
});
});
</script>
</head>
<body>
<h1>Enter a word:</h1>
<form action="index.php" method="post">
API Key: <input type="text" id="input" name="input"></br>
Concatenated Url + API: <input type="text" style="width:200px;" id="result" name="result" value="http//www.example.com/"></br>
</form>