I've been looking around stackoverflow for the answer to my problem but I can't seem to find why this is happening.

我一直在寻找stackoverflow来解决我的问题,但我似乎无法找到为什么会发生这种情况。

I've created a webserver which returns a JSON object:

我创建了一个返回JSON对象的Web服务器:

http://213.125.101.19/api.php?function=test

After that i created an HTML file with the following javascript to call the JSON using Ajax

之后,我使用以下javascript创建了一个HTML文件,使用Ajax调用JSON

<script language="javascript" type="text/javascript">
    <!-- 
    //Browser Support Code
    function ajaxCall(){
        var ajaxRequest;  // The variable that makes Ajax possible!

        try{
            // Opera 8.0+, Firefox, Safari
            ajaxRequest = new XMLHttpRequest();
        } catch (e){
            // Internet Explorer Browsers
            try{
                ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try{
                    ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e){
                    // Something went wrong
                    alert("Your browser broke!");
                    return false;
                }
            }
        }

        // Create a function that will receive data sent from the server
        ajaxRequest.onreadystatechange = function(){
            if(ajaxRequest.readyState == 4){
                var response = ajaxRequest.responseText;
                obj = JSON.parse(response);
                console.log(obj);

                if(response.indexOf("Fatal error")>=0){
                    alert('Error, Try again.');
                }else{
                    document.getElementById("response").value = response;               
                }                 

            }   
        }


            ajaxRequest.open("GET", "http://213.125.101.19/api.php?function=test", true);
            ajaxRequest.send(null); 
    }
    </script>

When I run this code, my Firebug returns

当我运行此代码时,我的Firebug返回

"SyntaxError: JSON.parse: unexpected end of data obj = JSON.parse(response);"

“SyntaxError:JSON.parse:意外的数据结束obj = JSON.parse(response);”

If I run my JSON trough a validator, everything seems to be fine.

如果我通过验证器运行我的JSON,一切似乎都没问题。

Any ideas how to fix this?

任何想法如何解决这一问题?

Kind Regards, Luuk

亲切的问候,Luuk

2 个解决方案

#1


0

JSON looks simple enough. I guess there's something wrong with the call or the return value.

JSON看起来很简单。我猜调用或返回值有问题。

Check status and responseType as well

检查状态和responseType

ajaxRequest.onreadystatechange = function(){
    if(ajaxRequest.readyState == 4){
        console.log('status=' + ajaxRequest.status +
                    ', statusText=' + ajaxRequest.statusText +
                    ', responseType=' + ajaxRequest.responseType +
                    ', responseText=' + ajaxRequest.responseText);
...
}

更多相关文章

  1. 如何对继承对象进行单元测试?
  2. PHP中类和对象的相关函数
  3. 【PHP面向对象(OOP)编程入门教程】15.static和const关键字的使用
  4. PHP面向对象笔记 —— 113 封装概念
  5. PHP的语言特性-面向对象和C++/java/python的相似之处
  6. php面向对象之抽象类和接口理解
  7. 键入提示 - 指定对象数组
  8. PHP 创建对象的两种方法
  9. Python:我如何从datetime.timedelta对象中获取时间?

随机推荐

  1. Android SDK Manager无法更新解决方法
  2. Android常用网站
  3. Android屏蔽返回键
  4. android 技术大牛 BLOG
  5. Android Studio提升编译速度
  6. Android拨号键盘声音源码位置
  7. 一起Talk Android吧(第二百五十九回:Androi
  8. 日期选择控件
  9. android camera [一] Camera open proces
  10. 2013.08.21——— android 四种启动模式