JSON API to HTML - json

I try using different instructions to use a JSON API from a Wordpress-System in a HTML-Teamplate. Unfortunately I do not succeed. Does anyone have any idea how I can read the section "Content" of http://www.earnyour21.de/api/get_page/?id=1588?
blog: function () {
$.ajax({
url: 'http://www.earnyour21.de/api/get_page/?id=1588',
type: 'GET',
dataType: 'json',
success: function (data) {
console.log(data);
},
error: function (data) {
console.log(data);
}
});
}

If the data structure of the JSON will always be the same, you can simply access the object directly using the objects name in JS.
blog: function(){
$.ajax({
url: 'http://www.earnyour21.de/api/get_page/?id=1588',
type: 'GET',
dataType: 'json',
success: function(data){
$('#content_test').append(data['page']['content']);
},
error: function(data){
$('#content_test').append(data['page']['content']);
}
});
}
Basically you need to use jquery to grab the div with an id of content_test and then append your data from the json. http://api.jquery.com/append/ and http://www.json.com/ for further reference.

Related

Display Json data with ajax in codeignitor 4

My JSON data is available in browser console. But i cannot display data in template.
<script type="text/javascript">
$(document).ready(function () {
$.ajax({
url: '<?php echo_uri("clients/session_details") ?>',
type: 'GET',
dataType: 'json',
success: function(response) {
var data = JSON.parse(response);
$("#result").html(data[0].rx-byte);
}
});
});
</script>
Based on the JSON data you provided, it seems like you are trying to display the rx-byte value in the HTML template. Here's a code sample that will do that:
$(document).ready(function () {
$.ajax({
url: '<?php echo_uri("clients/session_details") ?>',
type: 'GET',
dataType: 'json',
success: function (response) {
$("#result").html(response[0]["rx-byte"]);
}
});
});

Json does not write the returned object to the console

I have this piece of code that consumes a restful API using ajax. I am just trying to write on the console the returned data from the API, to my surprise it does not show any data, what's to be corrected?
$.ajax({
type: "get",
url: "http://localhost:8800/dialects",
crossDomain: true,
dataType: 'jsonp',
success: function(data){
console.log(data);
}
});
When I query the endpoint directly from the browser it returned the object's endpoint: http://localhost:8800/dialects. Sample of returned objects:
[{"glottocode":"zyud1238","names":"Zyuzdin","isocodes":"","macroarea":"Eurasia"},{"glottocode":"zwal1238","names":"Zwall","isocodes":"","macroarea":"Africa"},{"glottocode":"zuwa1238","names":"Zuwadza","isocodes":"","macroarea":"Papunesia"},{"glottocode":"zuti1239","names":"Zutiua","isocodes":"","macroarea":"South America"},]
Try using content-type attribute in ajax
$.ajax({
url: "http://localhost:8800/dialects",
type: "get",
crossDomain: true,
contentType: "application/jsonp; charset=utf-8",
dataType: 'jsonp',
success: function(data){
console.log(data);
}
});

Json JQuery error for no reason

I get an with no description from ajax:
#section Scripts {
<script type="text/javascript">
$(document).ready(function () {
$.ajax({
url: 'http://www.ouzhat.com/madad/api/adsapi',
type: 'POST',
dataType: 'json',
contentType: "application/json; charset=utf-8",
success: function (data) {
},
error: function (x,y,z) {
alert(x+'\n'+y+'\n'+z);
}
});
});
</script>
This is my webapp method code:
public JsonResult get() {
return new JsonResult() {
Data=ADS.SelectAll(),
JsonRequestBehavior=JsonRequestBehavior.AllowGet
};
}
It is cross domain issue (Please read on internet about it for further clarification).
if ajax and service file is on same domain then remove http://www.ouzhat.com from URl and provide local reference.
Another way is to add header [ header("Access-Control-Allow-Origin: *"); (example of PHP)] to your service file.

Chrome extension transfer to manifest 2 - No ajax response

Hayush!
Been trying to transfer my Chrome extension to manifest 2. Everything is working except for one script which calls an ajax. I have a variable that contains a JSON content which needs to be transferred to the server.
function sendlist(list){
jsontext = JSON.stringify(list);
$.ajax({
url: amfurl + "user/listbookmarks/",
dataType: 'text json',
async: true,
type: 'POST',
processData: false,
data: {'folders': jsontext},
success: function(data){
$('#importing').css('display','none');
$('#importdone').css('display','block');
console.log(data);
},
error: function(xhr, status, error) {
var err = eval("(" + xhr.responseText + ")");
alert(err.Message);
}
});
}
For some reason the ajax part is not executed at all. No error message was triggered, nor in the server error log.
All of the inline scripts and scripts in general were included in the popup.js. So it's probably not the issue.
Any thoughts?
Thanks!
The following code works perfectly on previous manifest
function importbookmarks(){
$('#formadd').css('display','none');
$('#importing').css('display','block');
_gaq.push(['_trackEvent', 'chromextension','importbookmarks', username]);
chrome.bookmarks.getTree(function(bookmarks) {
sendlist(bookmarks);
});
}
function sendlist(list){
jsontext = JSON.stringify(list);
$.ajax({
url: amfurl + "user/listbookmarks/",
dataType: 'text json',
async: true,
type: 'POST',
// processData: false,
data: {'folders': jsontext},
success: function(data){
$('#importing').css('display','none');
$('#importdone').css('display','block');
console.log(data);
}
});
}
The problem wasn't with the function. The button execution wasn't calling it correctly.
Here is the code I used to get the function to work in manifest 2
document.addEventListener("DOMContentLoaded", function () {
document.getElementById("import-bookmarks").addEventListener("click", function () {
importbook();return;
});

JSON data not parsing in node.js displays undefined in console

JSON data not parsing in node.js displays undefined in console..
Here is HTML code:
var jsonObjects = [{id:1, name:"amit"}];
jQuery.ajax({
url: 'http://localhost:8081',
type: "POST",
data: {"id":"1", "name":"amit"},
dataType: "json",
success: function(result) {
alert("done")
}
});
Here is Nodejs code:
http.createServer(function (request, response)
{
response.writeHead(200, {"Content-Type":"text/plain"});
var urlObj = JSON.stringify(response.data, true);
console.log(urlObj)
response.end();
}).listen(8081);
Try using GET method Instead of Post. Try this
var jsonObjects = [{id:1, name:"amit"}];
$.ajax({
type: 'GET',
url: 'http://localhost:8081',
data: {
jsonData: JSON.stringify(jsonObjects)
},
dataType: 'json',
complete: function(validationResponse) {
}
});
The data will be held in the request, not the response as it has come from the client request.
If you are using express to create your http server you will also need to tell it to use the bodyParser middle wear.