任何人都可以请检查可能是什么问题。我已经添加了我的API键以及...我有一个以下的代码调用API和显示API响应没有刷新页面。它似乎没有工作,因为现在:(
我是通过一个简短的代码添加到WordPress页面。一直在努力让这件事做的权利,但似乎不能这样做。任何帮助将不胜感激。
<!DOCTYPE html>
<html>
<head>
<title>Ajax form submit without reloading page</title>
<style type="text/css">
form {
text-align: center;
padding: 26px;
margin: 11px;
}
div {
padding: 15px;
}
h1 {
text-align: center;
}
</style>
</head>
<body>
<h1>This is the simple form using Ajax</h1>
<form method="POST" action="#">
<div>
<label>Name</label>
<input type="text" id="name" name="name">
</div>
<button type="submit">Submit</button>
</form>
</body>
</html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('button').click(function(event){
event.preventDefault();
var name = $('#name').val();
$.ajax({
URL: "https://api.openai.com/v1/engines/davinci/completions",
type: "POST",
data: JSON.stringify({
"prompt": "name",
"max_tokens": 10,
"temperature": 0.7,
"top_p": 1.0
}),
headers: {
'Authorization': 'Bearer sk-MY API KEY',
'Content-Type': 'application/json'
},
success: function(response) {
console.log(response);
$("#completion").html(response.choices[0].text);
}
});
});
});
</script>
1条答案
按热度按时间4dbbbstv1#
您应该将
<script>
标记放在标头中