2014-12-06 72 views
0

我正在學習ajax,jquery和json。JS小提琴JSON/ECHO返回空對象

我有以下JS提琴發送請求到JSON/ECHO和響應是一個空對象。誰能告訴我我做錯了什麼?

http://jsfiddle.net/deandalby/7a2t0eb5/3/

var saveUrl = "http://fiddle.jshell.net/echo/json/"; 

$(document).ready(function() { 
    $("#saveButton").click(function() { 
     Save(); 
    }); 
}); 

function GetPersonDetails() { 
    var arrayx = $(":input").serializeArray(); 
    var json = {}; 
    jQuery.each(arrayx, function() { 
     json[this.name] = this.value; 
    }); 

    writeToDom('Formatted JSON', JSON.stringify(json, null, 4)); 

    return json; 
} 

function Save() { 
    var data = GetPersonDetails(); 

    $.ajax({ 
     url: saveUrl, 
     dataType: "JSON", 
     data: data, 
     type: "POST", 
     cache: false, 
     success: function (response) {  
     writeToDom('Plain Response', JSON.stringify(response)); 
     writeToDom('Formatted Response', JSON.stringify(response, null, 4)); 
     }, 
     error: function (response) { 
     alert("error"); 
     }, 
     complete: function() { 
     writeToDom("complete", ""); 
     } 
    }); 
} 

function writeToDom(title, content) { 
    $("form").append("<div class='alert alert-success' role='alert'>" + title + ":</div><div><pre>" + content + "</pre></div>"); 
} 

回答

0

爲了得到的jsfiddle echo阿賈克斯工作,你已經將數據發送爲字符串。此外路徑/echo/json/將尋找一個崗位關鍵json和字符串的值將是

變化

var data = GetPersonDetails(); 

var data = {json: JSON.stringify(GetPersonDetails())}; 

如果你是使用HTML這將是:

var data ={ html:'<p>Some text</p>'}; 

而路徑將是/echo/html/

DEMO