调用API时,Ajax success()无效

时间:2017-02-21 19:07:05

标签: javascript jquery ajax

按钮的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);
    }
  });
  });
});

1 个答案:

答案 0 :(得分:5)

你有两个问题:

  1. 标题应为spring.resources.cache-period
  2. 它是headers而不是JSON.parse
  3. 
    
    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);
    			}
    		});
    	});
    });