如何在JavaScript警报中显示instagram API响应? [英] How to display instagram API response in javascript alert?

查看:74
本文介绍了如何在JavaScript警报中显示instagram API响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to send a delete request to instagram api using ajax but my current javascript code display the response in alert box as:

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

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

so i cant find the problem why delete request is not successful! if i call the same php script(doit.php) from the browser and pass it media id then the delete will be successful!

有人可以告诉我我当前的删除ajax请求出了什么问题,但对instagram api却没有做出删除请求吗?

could any one tell me what is wrong with my current delete ajax request that fails to make delete request to instagram api?

用于删除喜欢的Instagram API文档

示例api响应:

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

javascript:

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;
}

推荐答案

$ MediaIDvar = $ _POST ["MediaID"];

$MediaIDvar= $_POST["MediaID"];

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

$MediaIDvar= $_GET["MediaID"]; // => this will overwrite the previous $MediaIDvar

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

it will work if you call the php script with a GET variable, not through ajax using POST ;-)

这篇关于如何在JavaScript警报中显示instagram API响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆