2016-07-18 59 views
0

我目前正在撰寫註冊表格。註冊成功後,我的服務器發回一個HTML文件,我希望AJAX轉到登錄HTML。AJAX接收HTML文件

有人可以幫助我與AJAX和JQUERY PLZ?

這是我有:

 $.ajax({ 
             type: 'POST', 
             url: "http://localhost:3000/UserRegistration", 

             dataType: 'text', 
             data: FormData, 
             success: function (response) 
             {} 

而且什麼的dataType?這是我發送到網址或接收的數據類型。因爲我正在發送JSON文件並接收HTML?

你是否也知道如何在新加載的頁面上運行腳本?例如,我想加載登錄頁面,並在頁面上添加一個「TickMark」圖像:$ ApprovedTick.insertAfter('header');但這不會在新頁面上工作

+0

您需要將您的dataType更改爲「JSON」 –

+0

'dataType' =「您期待從服務器返回的數據類型。」 - [ajax()](http://api.jquery.com/jquery.ajax/)。 – showdev

+0

使用JSON不起作用。我發送JSON到服務器並接收一個html文件,所以它應該保持文本正確? – Ben

回答

1

在ajax成功事件中,使用window.location重定向到登錄頁面。

enter code here 

success: function (response) { 
     window.location = "http://localhost:3000/login.html"; 
} 
+0

謝謝!你還知道如何在新加載的頁面上運行腳本嗎? 例如,我想加載登錄頁面,並在頁面上添加一個「TickMark」圖片:$ ApprovedTick.insertAfter('header');但這不會在新頁面上工作 – Ben

+0

對於圖像部分,您需要在'http:// localhost:3000/login.html'頁面中添加腳本標記。 –

+0

使用jquery文檔已準備好的事件:https://learn.jquery.com/using-jquery-core/document-ready/ –

0

嘗試將dataType設置爲html。

$.ajax({ 
    type: 'POST', 
    url: "http://localhost:3000/UserRegistration", 
    dataType: 'html', 
    data: FormData, 
    success: function (response){ 
    //supposing you have an html element where you want to append 
    //the response, with an id like appendResponse 
    $('#appendResponse').html(response); 
    } 
});