Reputation: 2618
I'm using jQuery and the console returns me:
Data Loaded: [object Object]
The 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);
});
});
how I can convert the result of 'json' in a "string". thanks.
Upvotes: 0
Views: 108
Reputation: 2618
which returns json is incorrect. By clicking "submit" the form and hopes to return a JSON. form is returned.
the script is correct?
$("#ajaxForm").submit(function(e){
// disable the form submit
e.preventDefault();
});
in this case the events do not work, but the submit button, does not work.
The 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>
Upvotes: 0
Reputation: 17
To convert JSON Object to String, use the below code.
JSON.stringify(data);
"data" is your JSON object.
To convert back string into JSON Object, use the below code.
JSON.parse(String);
Upvotes: 1
Reputation: 401
$.post(url, returnFunction(data){...}, 'text');
will ensure that data
is in text format, but if you instead do the following:
$.post(url, returnFunction(data){...}, 'json');
Then jQuery will automatically ensure data
is a JSON object.
Note, jQuery will try to intelligently guess what the data is and pass back the correct object, putting 'text' or 'json' just makes this explicit.
Upvotes: 0