JQuery帖子无法正常工作

时间:2013-06-26 18:12:30

标签: jquery

我发疯了,我不知道为什么但是我非常简单的Jquery帖子无法正常工作。 我有index.php和temp.php如下。我仍然得到“NOT RETURN OK”,不知道为什么因为如果我在if条件之前提醒它,数据会返回“ok”。

的index.php:

<? session_start(); ?>
<!DOCTYPE HTML>
<meta charset="windows-1250">

<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-   ui.css" rel="stylesheet" type="text/css"/>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js">              </script>
 <link rel="stylesheet" type="text/css" href="style.css" />

 <script>

function do_post(item)
{     
  $.post("temp.php", {par:item}, function(data,status){
  //alert("Data: " + data + "\nStatus: " + status);
  if(data=="ok")
  {
   alert('RETURNED OK');
   //location.reload();
  }
  else
  {
   alert('NOT RETURNED OK');
  }
 });
}

</script>

<body>
<a href="#" onclick="do_post('whatever');">click</a>
</body>
</html>

temp.php:
<? echo "ok"; ?>

提前感谢任何想法。

2 个答案:

答案 0 :(得分:0)

我建议您在响应中使用JSON并使用正确的响应标头内容类型,或明确告诉jQuery .post您期望的内容:

$.post("temp.php", {par:item}, function(){}, 'text');

如果您只是在PHP脚本中回显,则不要在标题中指定内容类型,这可能无法正确解析jQuery,因为它必须猜测它正在使用的内容。还要寻找换行符。

答案 1 :(得分:0)

我已经在我的localhost上运行了代码并且它运行正常。然而,我的服务器没有解释php开放标签,我不得不向他们添加单词php