2016-08-10 20 views
1

在jQuery中使用AJAX時,我有奇怪的情況。 我在後端側使用的node.js與express.js一起,我寫了我的後端在這裏簡單的途徑:成功處理程序後AJAX parseerror被稱爲

app.all('/*', (request, response, next) => { 
    response.header('Access-Control-Allow-Origin', '*'); 
    response.header('Access-Control-Allow-Methods', 'POST,GET'); 
    response.header('Access-Control-Allow-Headers', 'Content-Type,accept,access_token,X-Requested-With'); 
    next(); 
}); 

app.post('test', (request, response) => { 
    response.json(
     { 
      "message": "success", 
      "data" : "test" 
     } 
    ); 

    // I also tried to chain .status(200) before calling .json(), the problem still occur 
}); 
現在在前端

,我用下面的代碼來執行的請求:

export class TestPage { 
    constructor() { 
     this._name = 'test'; 
    } 

    get name() { 
     return this._name; 
    } 

    performRequest() { 
     $.ajax({ 
      type: 'POST', 
      url: 'localhost:15000/test', 
      data: { "test": "nothing" }, 
      dataType: 'json', 
      success: (response) => { 
       console.log(response); 
      }, 
      error: (xhr, status, err) => { 
       console.log(xhr.responseText); 
       console.log(status); 
       console.log(err); 
      } 
     }); 
    } 
} 

,並呼籲從我的主這樣的腳本類:

import { TestPage } from './TestPage' 

    let test = new TestPage(); 
    test.performRequest(); 

現在AJAX執行請求後,它會觸發成功回調,然而,錯誤回調被觸發爲 好。

這裏我從控制檯窗口有: Console Window

正如你可能已經注意到,第一個日誌來自成功的回調外,其餘都來自錯誤回調。

任何想法發生在這裏以及如何解決這個問題?感謝

---- ----編輯
我找到錯誤的根源,所以在短期我爲了使用ES6功能使用巴貝爾和我使用類客戶端上的

如果我謹請求構造器,如:

export class TestPage { 

    constructor() { 
     this._name = 'test' 
     $.ajax({ 
      type: 'POST', 
      url: 'localhost:15000/test', 
      data: { "test": "nothing" }, 
      dataType: 'json', 
      success: (response) => { 
       console.log(response); 
      }, 
      error: (xhr, status, err) => { 
       console.log(xhr.responseText); 
       console.log(status); 
       console.log(err); 
      } 
     }); 
    } 

    get name() { 
     return this._name; 
    } 
} 

,並稱之爲:

let test = new TestPage(); 

它的工作原理!沒有錯誤回調被調用。

現在我的問題,這是如何發生的?爲什麼?以及如何將請求調用分離到一個函數?在構造函數中調用請求並不是我想要的,因爲可能我想在我的類的某處封裝請求。

+3

你是說成功和錯誤回調觸發一個AJAX?這絕不應該發生。這些日誌可能來自不同的Ajax請求。 – Ozan

+0

我同意@Ozan,...也嘗試使用函數(){}而不是()=> {} –

+0

都來自一個ajax,我試圖添加一些日誌,以確保它來自一個ajax也添加beforeSend參數並記錄下來,實際上它來自一個ajax請求.. 之前發送也調用過一次 – tronic

回答

1

我無法重現該問題(我使用babel.js啓用ES6),我懷疑你在這裏發佈的代碼是你的實際代碼。

但在這裏我的建議:

  1. 確保日誌來自相應的Ajax請求,而不是從另一個請求。 (就像@Ozan說的,仔細檢查一下)
  2. 確保你的回調不會引發錯誤。 (如果您的實際代碼與本文不同)
  3. 嘗試對計劃javascript發出請求。
  4. 嘗試從服務器端日誌request對象,也許你會發現
  5. 醜了一點有用的東西,但你可以嘗試手動解析響應,刪除dataType: 'json'和使用JSON.parse(response)在客戶端檢索JSON對象,使用try-catch塊可能會有幫助,因爲它無法解析它。

但是就像@ozan說的那樣,這樣的事情永遠不會發生,所以發佈你的實際/完整的代碼以防萬一它不同。

+0

你;對了,我試着簡化這裏的事情,但由於某種原因,在'成功'回調中引發錯誤,使用'try catch'來檢測源的錯誤並刪除相應的代碼可以解決問題。另外,你的第5個關於手動解析的建議也可以工作(甚至在'success'回調中拋出錯誤)。現在它是有道理的,非常感謝! – tronic

0

在node.js中端的代碼,請與response.send嘗試()而不是response.json()

app.post('test', (request, response) => { 
    response.send({"message": "success","data" : "test"}); 
}); 
+0

不幸的是,它不工作,看我的編輯 – tronic

相關問題