I use ajax to get json from a web API as following.
The browser log shows that json result is extracted, but the page returned the error function: alert("Please try later"). browser log image Is there anything wrong?
Thanks!
$.ajax({
url :"http://dgidb.genome.wustl.edu/api/v1/interactions.json",
type:"get",
async:false,
data: {genes: genes, interaction_sources: interaction_sources},
dataType:"jsonp",
jsonp:"callback",
jsonpCallback:"message",
success: function(data){
alert(data);
},
error: function(){
alert("Please try later");
}
});
Remove dataType:"jsonp" or use dataType:"json"
Using your code I could not get it to work unless I added quotes around the "genes" and "interaction_sources".
If I access the url: http://dgidb.genome.wustl.edu/api/v1/interactions.json directly it says: {"error":"You must enter at least one gene name to search!"}
Perhaps instead of the "genes" and "interaction_sources" you should be using search parameters?
EDIT: if you could provide the API for the json you are trying to access we might be able to debug it?
$("#runme").on("click", function() {
RunMe();
});
function RunMe() {
$.ajax({
url: "http://dgidb.genome.wustl.edu/api/v1/interactions.json",
type: "get",
async: false,
data: {
genes: "genes",
interaction_sources: "interaction_sources"
},
dataType: "jsonp",
jsonp: "callback",
jsonpCallback: "message",
success: function(data) {
alert(data);
},
error: function() {
alert("Please try later");
}
});
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<button id="runme">RUN ME!!!</button>
Ensure API response date with application/json header
{
"matchedTerms": [],
"unmatchedTerms": [
{
"searchTerm": "GENES",
"suggestions": []
}
]
}
This response's Content-Type would be application/json. I tried this is on POSTMAP manually and it worked. Just fixed the Content-Type header value.