未处理AJAX响应

未处理AJAX响应

问题描述:

我已经成功创建了几个AJAX调用,但不知何故,以下简单的AJAX测试不起作用。我有两个变量,这些变量是通过输入表单设置的(这个工作)。我们称这些变量为input_1和input_2。我想将这些输入传递给一个php文件,php文件处理它并返回一些东西。最后,响应被设置在JS变量中。现在我将响应放在DOM元素中,以查看AJAX调用是否成功。不幸的是,它不是。未处理AJAX响应

我的代码:

<html> 
<head> 
</head> 
<body> 

<script type="text/javascript"> 

var input_1 = 'abc'; // this is input of the form 
var input_2 = 'def'; // this in input of the form 

function generate_content(x,y) 
{ 

$.ajax({ 
      url: "create_response.php", 
      type:"get", 
      data:{firstvar:x, secondvar:y} 
     }).success(function(response){ 
      document.getElementById("temp-id").innerHTML=response; 
     });   

} 

generate_content(input_1, input_2); 

</script> 

<p id="temp-id"></p> 

</body> 

</html> 

而且PHP文件create_response.php:

<?php 

$var1=$_GET['firstvar']; 
$var2=$_GET['secondvar']; 

echo $var1; 

?> 

所以,我希望显示在被称为P标签INPUT_1(= 'ABC')'临时ID”,因为

  • INPUT_1被发送到create_response.php作为firstvar
  • create_response.php得到firstvar并将其设置为$ VAR1
  • $ VAR1相呼应,这是成功的功能AJAX
  • 成功函数的响应显示在P标签“临时ID”

但事实并非如此。

有什么想法/建议吗?

+0

什么显示?如果什么都没有,那么可能解析或语法错误 –

+1

是你显示的文件的整个内容的HTML/JS代码?它看起来像你试图使用jQuery,但没有包括jQuery。 –

+0

您的代码不符合[JS代码风格标准。](http://www.w3schools.com/js/js_conventions.asp) 请养成遵循这些风格的习惯。 – Shadetheartist

AFAIK没有对$.deferred没有success方法,也许你应该试试.then.done

function generate_content(x,y) { 
    $.ajax({ 
     url: "create_response.php", 
     type:"get", 
     data:{firstvar:x, secondvar:y} 
    }).then(function(response){ 
     document.getElementById("temp-id").innerHTML=response; 
    });   
} 

您可以添加功能上设置的success属性对象,而不是使用像Ajax调用内,就像你现在一样。

$.ajax({ 
    url: "create_response.php", 
    type:"get", 
    data:{firstvar:x, secondvar:y}, 
    success: function(response){ 
     document.getElementById("temp-id").innerHTML=response; 
    } 
});