继续进行之前的API验证

时间:2016-02-19 17:45:52

标签: javascript php html ajax validation

我正在使用Instagram的api,我必须在提交表单之前验证个人资料:

<button type="button" id="clickMe">Run It!</button>
  <div id="data">
  </div>
<script type="text/javascript">
   $('#clickMe').click(function() {
      $.ajax({
         method: 'get',
         url: 'php-script.php',
         data: {
            'username': '217380759',
            'ajax': true
         },
         success: function(data) {
            $('#data').text(data);
         }
      });
   });
</script>

的PHP的script.php:

$access_token = "...";
$username = "...";
$url = 'https://api.instagram.com/v1/users/' . $username . '?access_token=' . $access_token;
$api_response = file_get_contents($url);
$record = json_decode($api_response);
$code = $record->meta->code;

if($code == 200) {
   //user is valid, continue to next page
} else {
   echo "invalid";
}

有关验证Instagram用户的任何建议,允许用户继续而不再次提交提交?

1 个答案:

答案 0 :(得分:1)

试试这段代码:

success: function(data) {
  if(data != 'invalid') {
      location.href = "YOUR NEXT PAGE URL";
  }
}
相关问题