ajax与JavaScript异常:意外的输入结束 [英] ajax with JavaScript exception: unexpected end of input

查看:291
本文介绍了ajax与JavaScript异常:意外的输入结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个概念的输入字段,当用户填写它时,他必须检查概念是否存在。所以我做了一个检查按钮,它使用ajax和JavaScript检查数据库,看看是否存在这个概念。我的问题是当使用ajax和JavaScript时我得到这个异常:

I have an input field for a concept and when the user fills it out, he has to then check if the concept exists. So I made a check button, which checks a database using ajax and JavaScript to see if the concept exists. My problem is when using ajax and JavaScript I get this exception:


意外的输入结束

unexpected end of input

JS:

var concept = document.getElementById('acConceptName').value;
    xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange=function(){
        if(xmlhttp.readyState==4 && xmlhttp.status==200){
            var isexisted = JSON.parse(xmlhttp.responseText);
            if(isexisted[0]==true){
                var errorMessage = document.getElementById('acSuggesConcepts');
                var p = document.createElement('p');
                p.innerHTML="this concept is already existed";
                errorMessage.appendChild(p);
                errorMessage.style.display="block";            
            }
        }
    }
    xmlhttp.open("GET","http://localhost/Mar7ba/Ontology/isExistedConcept/"+concept+"/TRUE",true);
    xmlhttp.send();

什么是异常,我该如何解决?

What is the exception and how can I solve it ?

PHP 函数检查数据库,我总是返回true

PHP : function to check database and I always return true in it

public function isExistedConcept($concpetName,$Ajax){
        if($Ajax==true){
            $results=true
             $d=array($results);
            return json_encode($d);
        }
 }

演示: http://jsfiddle.net/Wiliam_Kinaan/s7Srx/2/

推荐答案

查看代码一段时间后,可能是一个可疑的事情是你的PHP。

After looking at the code for a while, one thing that might be a suspect is your PHP.

您的函数在php以 return 命令结尾。 AJAX调用实际上正在等待的是一些要发回的数据。 return命令简单地将该值传递回原来调用该函数的实体。

Your function in php ends with a return command. What the AJAX call is actually waiting for is some data to be sent back. The return command simply passes that value back to the entity that originally called the function.

尝试将函数改为 echo 结果,而不是返回。保存您的返回值,当您需要的结果进入另一个PHP函数,而不是当您返回数据到客户端。

我只在这里返回命令为了可读性。

Try alter your function to echo the result as opposed to returning it. Save your return value for when you need the result to go into another PHP function, not when you are returning data to the client.
I only put this return command here for readability.

public function isExistedConcept($concpetName,$Ajax){
  if($Ajax==true){
    $results=true
    $d=array($results);
    echo json_encode($d);
  }
  return;
 }

这篇关于ajax与JavaScript异常:意外的输入结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆