调用API时,Ajax success()无效

aay*_*shi 2 javascript ajax jquery

按钮的onClick()事件没有指向任何地方.ajax的success()可能存在一些问题.我无法弄清楚我对此是什么新手.

var currentAuthor="";
var currentQuote="";
$(document).ready(function(){
  $("#getMessage").on("click",function(){
  $.ajax({
  header:{ 
      "X-Mashape-Key":"xE5Raw3acMmsh4dpp6HEk5WSbJtTp1X9TL3jsnue3VRzr5vNNa",
   Accept: "application/json",
      "Content-Type": "application/x-www-form-urlencoded"
  },
    url:"https://andruxnet-random-famous-quotes.p.mashape.com/?cat=",
    success: function(response){
      var r=json.parse(response);
      currentQuote=r.quote;
      currentAuthor=r.author;
       $("#author").html(r.author);
    }
  });
  });
});
Run Code Online (Sandbox Code Playgroud)

Z-B*_*one 5

你有两个问题:

  1. 标题应该是 headers
  2. JSON.parse不是json.parse

var currentAuthor = "";
var currentQuote = "";
$(document).ready(function () {
	$("#getMessage").on("click", function () {
		$.ajax({
			headers: {
				"X-Mashape-Key": "xE5Raw3acMmsh4dpp6HEk5WSbJtTp1X9TL3jsnue3VRzr5vNNa",
				"Accept": "application/json",
				"Content-Type": "application/x-www-form-urlencoded"
			},
			url: "https://andruxnet-random-famous-quotes.p.mashape.com/?cat=",
			success: function (response) {
				var r = JSON.parse(response);
				currentQuote = r.quote;
				currentAuthor = r.author;
				$("#author").html(r.author);
			}
		});
	});
});
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="getMessage">Get Message</button>
<p id="author"></p>
Run Code Online (Sandbox Code Playgroud)