2017-02-21 3 views
2

Das OnClick() Ereignis der Schaltfläche nicht leitet anywhere.There könnte ein Problem mit dem Erfolg sein() von ajax.I kann nicht herausfinden, was, wie ich bin neu dazu.Ajax Erfolg() funktioniert nicht, wenn API Aufruf

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); 
    } 
    }); 
    }); 
}); 
+0

Erhalten Sie irgendwelche Fehler in der Konsole? – Hodrobond

+0

Ich habe versucht, Ihren Code in eine Geige zu kopieren und ich habe eine 401, die Kopfzeile "X-Mashape-Key" wird nie hinzugefügt – Nicolas

+0

Also, was soll ich tun? @Hodrobond – aayushi

Antwort

5

Sie hatten zwei Probleme:

  1. Kopf headers
  2. es JSON.parse ist sein sollte und nicht json.parse

var currentAuthor = ""; 
 
var currentQuote = ""; 
 
$(document).ready(function() { 
 
\t $("#getMessage").on("click", function() { 
 
\t \t $.ajax({ 
 
\t \t \t headers: { 
 
\t \t \t \t "X-Mashape-Key": "xE5Raw3acMmsh4dpp6HEk5WSbJtTp1X9TL3jsnue3VRzr5vNNa", 
 
\t \t \t \t "Accept": "application/json", 
 
\t \t \t \t "Content-Type": "application/x-www-form-urlencoded" 
 
\t \t \t }, 
 
\t \t \t url: "https://andruxnet-random-famous-quotes.p.mashape.com/?cat=", 
 
\t \t \t success: function (response) { 
 
\t \t \t \t var r = JSON.parse(response); 
 
\t \t \t \t currentQuote = r.quote; 
 
\t \t \t \t currentAuthor = r.author; 
 
\t \t \t \t $("#author").html(r.author); 
 
\t \t \t } 
 
\t \t }); 
 
\t }); 
 
});
<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>

+0

es hat funktioniert! Vielen Dank! @ Z-Bone – aayushi

+0

@aayushi Sure Sache :) –

Verwandte Themen