duanhu7390 2018-04-13 17:01
浏览 35
已采纳

来自PHP文件的Ajax回调

I'm trying to pass two number and check if their product is true or false. I can see call made successfully in network tab and when i click that link, output is correct to. But i m stuck at retrieving that result. It doesn't show anything in data1.

function call(){
    console.log(fun);
$.ajax({
  url: "http://localhost/mt2/checkanswer.php",
  dataType: "jsonp",
  type: "POST",
           //window.alert("what");
  data: {
    num1:2,
      num2:2,
      answer:5

  },
  success: function( data1 ) {
     console.log(data1);
    $( "#timeDiv" ).html( "<strong>" + data1 + "</strong><br>");
  }

<?php

  // get two numbers and the answer (their product) and return true or false if the answer is correct or not. 
  // using this as an api call, return json data
  // calling <your host>/checkanswer.php?num1=4&num2=5&answer=20 will return true
  // calling <your host>/checkanswer.php?num1=4&num2=5&answer=21 will return false

  if(isset($_GET['num1']) && isset($_GET['num2']) && isset($_GET['answer']) && is_numeric($_GET['num1']) && is_numeric($_GET['num2']) && is_numeric($_GET['answer'])) {

    $product = $_GET["num1"] * $_GET["num2"];

    if ($product === intval($_GET['answer'])) {
      $result = true;
    } else {
      $result = false;
    }
    header('Content-type: application/json');
    echo json_encode($result);

  } 
?>

https://drive.google.com/open?id=1ocF344ZxG3HXJR0WQha1kOoVM9bCepnI "console"

  • 写回答

1条回答 默认 最新

  • douzhigan1687 2018-04-13 17:29
    关注

    The issue is your Javascript is submitting the data via JS as a post request and your PHP is looking for a get request.

    if(isset($_GET['num1']) && isset($_GET['num2']) && isset($_GET['answer']) && is_numeric($_GET['num1']) && is_numeric($_GET['num2']) && is_numeric($_GET['answer'])) {
     ..
    }
    

    So either change method: 'POST' to method: 'GET' or change $_GET[..] to $_POST[..].

    Also that's one wild if statement. You could break it up so it's not so long and isn't as hard to read. This also allows you to add some additional information based on where your code 'fails.'

    if ( isset($_GET['num1'], $_GET['num2'], $_GET['answer']) ) {
    
      if ( !is_numeric([$_GET['num1'], $_GET['num2'], $_GET['answer']]) ) {
        // Our numbers aren't numeric!
        $message = 'Not all variables are numeric';
        $result = false;
      } else {
        $message = 'We did it!';
        $result = $_GET['num1'] + $_GET['num2'] == $_GET['answer'];
      }
    
    } else {
      // We didn't have all of our request params passed!
      $message = 'We didn\'t have all our variables';
      $result = false;
    }
    
    header('Content-type: application/json');
    echo json_encode([ 'message' => $message, 'result' => $result]);
    

    Edit

    Based on epascarello's comment remove dataType: 'jsonp'.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题