-1

我正在尝试使用 ajax 向 instagram api 发送删除请求,但我当前的 javascript 代码在警报框中显示响应为:

 <pre></pre><pre></pre>

所以我找不到删除请求不成功的问题!如果我从浏览器调用相同的 php 脚本(doit.php)并将其传递给媒体 ID,那么删除将成功!

谁能告诉我当前的删除 ajax 请求无法向 instagram api 发出删除请求有什么问题?

用于删除喜欢的 Instagram API 文档

示例 api 响应:

{
    "meta": {
        "code": 200
    }, 
    "data": null
}

javascript:

<script>
function deleteLike(a,b)
{
alert("MediaID:"+a+"\nId:"+b);

 var url = "./doit.php";
    $.post(url, {
        "method": "post",
      //"method": "delete",
        "MediaID": a, 
    }, function(data) 
       {
         var ajaxResponse = data;

          alert("Response:"+ajaxResponse);

          if (data.meta.code == 200) {
             alert("sucessDelete");


            }

    })

}
</script>

PHP代码:

$MediaIDvar= $_POST["MediaID"];
//$MediaIDvar= $_GET["MediaID"];

$url2 = "https://api.instagram.com/v1/media/".$MediaIDvar."/likes?xxxxx";
        $api_response2 = get_data2(''.$url2);
        $record2 = json_decode($api_response2); // JSON decode 



echo '<pre>' . print_r($api_response2, true) . '</pre>';
echo '<pre>' . print_r($record2, true) . '</pre>';
// to see what is in the $api_response and $record object



/* gets the data from a URL */
function get_data2($url2) {
    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}
4

1 回答 1

0

$MediaIDvar= $_POST["MediaID"];

$MediaIDvar= $_GET["MediaID"]; // => 这将覆盖之前的 $MediaIDvar

如果您使用 GET 变量而不是通过 ajax 使用 POST 调用 php 脚本,它将起作用;-)

于 2013-09-18T14:22:58.240 回答