简体   繁体   English

ajax调用失败,错误为“意外令牌”

[英]ajax calls fails with error “Unexpected token”

I have a php application that makes an ajax call and retrieves the following data from my database: 我有一个进行ajax调用并从数据库中检索以下数据的php应用程序:

{
    "5717a3873747106e60a087ec": {
        "_id": {
            "$id": "5717a3873747106e60a087ec"
        },
        "phone": "18455100020",
        "fax": false,
        "emergency": false,
        "site": "ABC",
        "sip_uri": "",
        "associated_users": [],
        "location": "New Zealand",
        "reservation": [{
            "reserved": true,
            "reserved_for": "COO12"
        }],
        "available": true
    }
}

This is the code that I have to make the ajax call: 这是我必须进行ajax调用的代码:

   $(document).ready(function(){
                var request = BASEPATH + 'index.php/export/get_widget/available';
                console.log(request);
                $.ajax({
                          url:request,
                          type:'GET',
                          dataType:'jsonp',
                          success: function(returnDataFromController) {
                                  if (returnDataFromController.status)
                                  { 
                                        console.log(returnDataFromController); 
                                        //build table contents 
                                  }
                          },// end success
                          error:  function(jqXHR, textStatus, errorThrown)
                          {
                                  console.log(errorThrown);
                          }
                });//end ajax.
        }); //end document ready

When I check the console, I see that the request failed with the following error message: 当我检查控制台时,我看到请求失败,并显示以下错误消息:

Uncaught SyntaxError: Unexpected token :

It's basically dying when it sees the first ":" after the mongo doc id "5717a3873747106e60a087ec" 当它在mongo doc id“ 5717a3873747106e60a087ec”之后看到第一个“:”时,基本上就快死了

What I've tried: 我尝试过的

I've validated the return data in jsonlint and it looks like the data is valid json. 我已经验证了jsonlint中的返回数据,并且看起来该数据是有效的json。

I'm not sure what else to check. 我不确定还需要检查什么。 I'm playing around with jsonp vs json for the data type of my ajax call. 我正在为我的ajax调用的数据类型使用jsonp vs json。 But other than that, I'm out of ideas. 但是除此之外,我没有主意。

Any suggestions? 有什么建议么?

EDIT 1 编辑1

I should also say that I found this post: jQuery.ajax() call is returning JSON.parse unexpected character error 我也应该说我找到了这篇文章: jQuery.ajax()调用返回JSON.parse意外字符错误

but i don't know how to edit the json headers. 但我不知道如何编辑json标头。 I'm currently also playing around with the code to see how to do this 我目前也在玩代码,看看如何做到这一点

EDIT 2: 编辑2:

When I change jsonp to json, I get another error message : 当我将jsonp更改为json时,出现另一条错误消息:

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.

The variable "request" contains something like this: 变量“ request”包含如下内容:

http://10.1.1.1/mytestapp/index.php/export/get_widgets/available

I thought the problem was that the above mentioned URL is being compared with the " http://localhost/mytestapp " URL that i used to launch the application. 我以为问题是上述URL与我用来启动该应用程序的“ http:// localhost / mytestapp ” URL进行了比较。 So tried to launch my web app like this instead: 因此,尝试像这样启动我的网络应用程序:

http://10.1.1.1/mytestapp/index.php/ http://10.1.1.1/mytestapp/index.php/

but it still fails with the same error. 但仍然失败,并显示相同的错误。

So the root cause of my problem was the json vs jsonp. 所以我的问题的根本原因是json vs jsonp。 I changed the data type in my ajax call to json. 我将ajax调用中的数据类型更改为json。 And then instead of passing the full URL in the call, I just passed the application name like this: 然后,我没有传递完整的URL,而是传递了这样的应用程序名称:

            var request = '/testapp/index.php/export/get_widgets/available';
154                 console.log(request);
155                 $.ajax({
156                           url:request,
157                           type:'GET',
158                           dataType:'json',
159                           success: function(returnDataFromController) {
160                                   if (returnDataFromController)
161                                   { 
162                                         console.log(returnDataFromController);
163                                         console.log(returnDataFromController.id); 
164                                         //build table contents 
165                                   }
166                           },// end success
167                           error:  function(jqXHR, textStatus, errorThrown)
168                           {
169                                   console.log(errorThrown);
170                           }
171                 });//end ajax.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM