问题
hi all could any one tell me how i can post a like and delete an instagram like using api? i tried to delete a like using this code but i dont get any response from ajax call. could any one tell me what i am doing wrong here ?is this possible doing php ?how?
xxxxxxxxxxxxxxxxxx_xxxxxxxx ==> Media-Id such as: 23424343243243_2343243243
Instagram API Docs for Deleting Like
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">
function deleteLike() {
alert("inside function");
var url = "https://api.instagram.com/v1/media/xxxxxxxxxxxxxxxxxx_xxxxxxxx/likes?access_token=XXXX";
$.post(url, {
"method": "delete",
}, function(data)
{
var ajaxResponse = data;
alert("success"+ajaxResponse);
})
}
</script>
</head>
<body onload="deleteLike();">
</html>
Edited:cross domain php curl:(but how to tell it if this is post method or delete method?"
$url = "https://api.instagram.com/v1/media/xxxxxxxxxxxxxxxxxx_xxxxxxxx/likes?access_token=XXXX";
$api_response = get_data(''.$url);
$record = json_decode($api_response); // JSON decode
/* gets the data from a URL */
function get_data($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
回答1:
EDIT: According to API specs, you need to use request of DELETE
type. To do this, specify {type: "DELETE"}
, not {"method": "delete"}
and use $.ajax()
function. See related question here: How to send a PUT/DELETE request in jQuery?
Are you sure you've got your POST request gets executed at all? It may be not running at all, please check requests in Developer Tools / Firebug / etc. And initialize your request jQuery way, don't use obtrusive JS:
$(function(){
deleteLike();
});
The way you use success
parameter you need to declare functions with more parameters: success(data, textStatus, jqXHR)
. This may also cause misbehavior. Please read docs here: http://api.jquery.com/jQuery.post/
I'd suggest you to use more handy .done()
function:
$.post(url, { "method": "delete"})
.done(function(data) {
var ajaxResponse = data;
alert("success: " + ajaxResponse);
})
.fail(function() {
// check for error here
});
You should also definitely be interested in checking .fail()
result - most probably you've missed something while preparing request.
来源:https://stackoverflow.com/questions/18852810/how-to-post-a-like-or-delete-an-instagram-like-using-instagram-api