ajax请求但是没有得到任何价值

时间:2016-11-19 14:40:55

标签: javascript jquery ajax

/* ----------- gradeMe -----------*/
function gradeMe () {
  console.log('I am here')
  var graderLink = 'https://inboundgrader.spacek.org/grader.php?website=' + window.Hayk.websiteUrl
  console.log(graderLink)
  $.ajax({
    method: 'GET',
    url: 'https://inboundgrader.spacek.org/grader.php?website=' + window.Hayk.websiteUrl
  }).success(function (data) {
    console.log(data)
    window.Hayk.grade = data.grade
    $('div').text(window.Hayk.grade)
  }).error(function (error) {
  $('div').text('error',error)
  console.log('error',error)
  })
}
window.Hayk = {}
window.Hayk.websiteUrl = 'incredo.co'
$('div').on('click', gradeMe)
div {
  width:100px;
  height:20px;
  display:flex;
  justify-content:center;
  align-items:center;
  background:black;
  color:red;
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div>clickMe</div>

大家好,我有这个功能。

/* ----------- gradeMe -----------*/
function gradeMe () {
  console.log('I am here')
  var graderLink = 'https://inboundgrader.spacek.org/grader.php?website=' + window.Hayk.websiteUrl
  console.log(graderLink)
  $.ajax({
    method: 'GET',
    url: 'https://inboundgrader.spacek.org/grader.php?website=' + window.Hayk.websiteUrl
  }).done(function (data) {
    console.log(data)
    window.Hayk.grade = data.grade
  })
  // $.get(graderLink, function (data) {
  //   console.log(data)
  //   window.Hayk.grade = data.grade
  // })
}

结束链接是这样的

https://inboundgrader.spacek.org/grader.php?website=www.incredo.co

我将此功能绑定到了一个点击。当我点击按钮时,我可以看到请求。在devtools网络选项卡中,我有文件。但是,控制台不记录任何数据,Hayk.gradeundefined

编辑:正如评论中所建议的那样,我将成功分解为成功和错误。并且所需的结果出现在错误中。因为这可能是因为php在这里。

<?php
header('Content-type: application/javascript');
header('Access-Control-Allow-Origin: *');
// Create a stream
$opts = array(
  'http'=>array(
    'method'=>"GET",
    'header'=>"Accept-language: en\r\n" .
              "Referer: https://website.grader.com/results/{$_GET["website"]}\r\n" .
              //"Cookie: __hluid=234b284c-639f-4ff9-8dcd-4a2ced9196d4\r\n" .
              "Origin: https://website.grader.com\r\n"          
  )
);

$context = stream_context_create($opts);


$content = file_get_contents("https://api.hubapi.com/websitegrader/v1/grade/" . $_GET["website"], false, $context);
if(isset($_GET["verbose"]) && $_GET["verbose"] == 1) {
    echo $content; exit;
}

$data = json_decode($content);
echo json_encode(["grade" => $data->grade]);
exit;

0 个答案:

没有答案