簡體   English   中英

ajax調用失敗,錯誤為“意外令牌”

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

我有一個進行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
    }
}

這是我必須進行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

當我檢查控制台時,我看到請求失敗,並顯示以下錯誤消息:

Uncaught SyntaxError: Unexpected token :

當它在mongo doc id“ 5717a3873747106e60a087ec”之后看到第一個“:”時,基本上就快死了

我嘗試過的

我已經驗證了jsonlint中的返回數據,並且看起來該數據是有效的json。

我不確定還需要檢查什么。 我正在為我的ajax調用的數據類型使用jsonp vs json。 但是除此之外,我沒有主意。

有什么建議么?

編輯1

我也應該說我找到了這篇文章: jQuery.ajax()調用返回JSON.parse意外字符錯誤

但我不知道如何編輯json標頭。 我目前也在玩代碼,看看如何做到這一點

編輯2:

當我將jsonp更改為json時,出現另一條錯誤消息:

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

變量“ request”包含如下內容:

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

我以為問題是上述URL與我用來啟動該應用程序的“ http:// localhost / mytestapp ” URL進行了比較。 因此,嘗試像這樣啟動我的網絡應用程序:

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

但仍然失敗,並顯示相同的錯誤。

所以我的問題的根本原因是json vs jsonp。 我將ajax調用中的數據類型更改為json。 然后,我沒有傳遞完整的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