2013-02-26 3 views
0

내가 jQuery를 사용하고 있는데 콘솔 나 반환개체를 문자열로 변환하는 방법은 무엇입니까?

Data Loaded: [object Object] 

jQuery를 :

$(document).ready(function(){ 
    //Button click event 

    $("#ajaxForm").submit(function(e){ 
     // disable the form submit 
     e.preventDefault(); 
    }); 
    /* get some values from elements on the page: */ 
    var $form = $(this), 
    term = $form.find('input[name="id"]').val(), 
    url = $form.attr('update'); 


    /* Send the data using ajax */ 
    var posting = 
     $.ajax({ 
      url: url 
     }); 

    $.post(url, { id: term }).done(function(data) { 
     console.log("Data Loaded: " + data); 
     $("#result").empty().append(data);  
    }); 
}); 

나는 "문자열"에서 'JSON'의 결과를 변환 할 수있는 방법에 대해 설명합니다. 감사.

+0

왜 그렇게하고 싶습니까? jQuery는 JSON => 객체를 변환하기위한 방법을 없앴습니다. 왜냐하면 그것이 거의 모든 시간에 필요하기 때문입니다. 그러나 어떤 경우 든 문자열을 원한다면'$ .post'의'dataType : 'text'옵션을 사용하면이를 막을 수 있습니다. – Jon

+0

로깅 목적으로 사용한다고 생각합니다. JSON이 단순히 Object라는 문구가 아니라 JSON의 내용을 원합니다. – Flater

+0

맞습니다. 로깅 용입니다. –

답변

2

JSON.stringify(data); 
0
$.post(url, returnFunction(data){...}, 'text'); 

data 텍스트 형식으로되어 보장합니다 시도하지만, 당신이 대신 할 경우 다음

$.post(url, returnFunction(data){...}, 'json'); 

그리고 jQuery를 자동으로 확인됩니다 data는 JSON 객체입니다 .

참고, jQuery는 데이터가 무엇인지 정확하게 파악하고 올바른 개체를 전달하려고 시도합니다. '텍스트'또는 'json'을 넣으면이 것이 명확하게 처리됩니다.

1

JSON 개체를 문자열로 변환하려면 아래 코드를 사용하십시오.

JSON.stringify(data); 

"data"는 JSON 개체입니다.

문자열을 JSON Object로 변환하려면 아래 코드를 사용하십시오.

JSON.parse(String); 
+0

JSON.parse (String); "문자열"이 경우에있을 것입니다. –

+0

"stringify"를 사용하여 원하는대로 객체를 문자열로 변환 할 수 있습니다. – Aleem

0

json이 올바르지 않습니다. 양식을 "제출"하고 JSON을 반환하기를 희망합니다. 양식이 반환됩니다.

스크립트가 정확합니까?

$("#ajaxForm").submit(function(e){ 
    // disable the form submit 
    e.preventDefault(); 
}); 

이 경우 이벤트는 작동하지 않지만 제출 버튼은 작동하지 않습니다.

form.html.

< form action = "update" method = "post" id = "ajaxForm" > 
     < fieldset class = "success" > 
     <legend> The fields marked with * . < /legend> 
     <div> 
     < label for = "destination" class = "block" > 
     host: 
     < span class = "mandatory_asterisk" > * < /span> 
     < /label> 
     < select id = "destination" class = "select" name = "destination" > 
     < option value = "element1" > element1 < /option> 
     < option value = "element2" > element2 < /option> 
     < /select> 
     < /div> 
     <div> 
     < span class = "infoTrigger" > Update a page of the search < /span> 
     < label for = "id" class = "block" > 
     page 
you want to index: 
     < /label> 
     < input type = "text" class = "text" id = "page" name = "page" 
     value = ""/> 
     < /div> 
     <div> 
     < span class = "infoTrigger" > Update a certain id of the host < /span> 
     < label for = "id" class = "block" > 
     id 
on the host (e.g. element1 - id - post - json): 
     < /label> 
     < input type = "text" class = "text" id = "id" name = "id" 
     value = ""/> 
     < /div> 
     < !-- < input id = "operation" type = "submit"/> -- > 
     < input type = "submit" value = "update" name = "update" id = "operation"/> 
     < /fieldset> 
     < div id = "anotherSection" > 
     <fieldset> 
     <legend> Response from jQuery Ajax Request < /legend> 
     < div id = "result" > < /div> 
     < /fieldset> 
     < /div> 
     < /form> 
관련 문제