wordpress 我有一个下面的代码,调用API并显示API响应,而不刷新页面,它似乎没有工作,因为现在:(

mitkmikd  于 2022-11-02  发布在  WordPress
关注(0)|答案(1)|浏览(186)

任何人都可以请检查可能是什么问题。我已经添加了我的API键以及...我有一个以下的代码调用API和显示API响应没有刷新页面。它似乎没有工作,因为现在:(
我是通过一个简短的代码添加到WordPress页面。一直在努力让这件事做的权利,但似乎不能这样做。任何帮助将不胜感激。

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Ajax form submit without reloading page</title>
  5. <style type="text/css">
  6. form {
  7. text-align: center;
  8. padding: 26px;
  9. margin: 11px;
  10. }
  11. div {
  12. padding: 15px;
  13. }
  14. h1 {
  15. text-align: center;
  16. }
  17. </style>
  18. </head>
  19. <body>
  20. <h1>This is the simple form using Ajax</h1>
  21. <form method="POST" action="#">
  22. <div>
  23. <label>Name</label>
  24. <input type="text" id="name" name="name">
  25. </div>
  26. <button type="submit">Submit</button>
  27. </form>
  28. </body>
  29. </html>
  30. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  31. <script type="text/javascript">
  32. $(document).ready(function(){
  33. $('button').click(function(event){
  34. event.preventDefault();
  35. var name = $('#name').val();
  36. $.ajax({
  37. URL: "https://api.openai.com/v1/engines/davinci/completions",
  38. type: "POST",
  39. data: JSON.stringify({
  40. "prompt": "name",
  41. "max_tokens": 10,
  42. "temperature": 0.7,
  43. "top_p": 1.0
  44. }),
  45. headers: {
  46. 'Authorization': 'Bearer sk-MY API KEY',
  47. 'Content-Type': 'application/json'
  48. },
  49. success: function(response) {
  50. console.log(response);
  51. $("#completion").html(response.choices[0].text);
  52. }
  53. });
  54. });
  55. });
  56. </script>
4dbbbstv

4dbbbstv1#

您应该将<script>标记放在标头中

相关问题