我正在使用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 :(得分:2)
试试这个
JSON.stringify(data);
答案 1 :(得分:1)
要将JSON对象转换为String,请使用以下代码。
JSON.stringify(data);
“data”是您的JSON对象。
要将字符串转换回JSON对象,请使用以下代码。
JSON.parse(String);
答案 2 :(得分:0)
$.post(url, returnFunction(data){...}, 'text');
将确保data
采用文本格式,但如果您改为执行以下操作:
$.post(url, returnFunction(data){...}, 'json');
然后jQuery会自动确保data
是一个JSON对象。
请注意,jQuery将尝试智能地猜测数据是什么并传回正确的对象,将'text'或'json'设为明确。
答案 3 :(得分:0)
不正确。点击"提交"形式和希望返回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>