How to use POST in ajax - html

I created a page where after loading the data will appear. I am using github url and ajax POST. But the data is not showing. What should I fix from the code that I have made?
<div id="content">
</div>
window.onload = function(){
$.ajax({
type: 'POST',
url: 'https://raw.githubusercontent.com/bimobaskoro/hewan/main/kucing.json',
dataType: 'application/json',
sucess: function (data) {
html = '<div class="content" id="content"></div>'
html += '<h1>' + data.judul + '</h1>'
html += '<p>' + data.isi + '</p>'
html += '</div>'
document.getElementById('content').innerHTML += html;
},
error: function() {
}
});
}

Consider the following.
$(function(){
$.getJSON("https://raw.githubusercontent.com/bimobaskoro/hewan/main/kucing.json", function (data) {
var html = $("<div>", {
class: "content"
});
$("<h1>").html(data.judul).appendTo(html);
$("<p>").html(data.isi).appendTo(html);
$("#content").append(html);
});
}
As GitHub is not apart of the same domain, you may encounter CORS issues. If so, you might consider JSONP.

Why would you use post to a json file. Get method is enough.
$.ajax({
url: 'https://raw.githubusercontent.com/bimobaskoro/hewan/main/kucing.json',
success: function (data)
{
var html = '<div class="content" id="content">';
var obj=JSON.parse(data);
obj.forEach((e)=>{
html += '<h1>' + e.judul + '</h1>'
html += '<p>' + e.isi + '</p>'
});
html += '</div>'
document.getElementById('content').innerHTML = html;
}
});

Related

Pass parameter to href of a table cell in ajax

I'm new to use ASP.net for web app programming. I'm using the following ajax function aiming to send a parameter to the controller with the name of yyy. However, no matter how I manipulate the variable {id} in this case, the controller either get the string {id} or null. The variable of {id} is actually storing an integer value, for example, 3. I want to pass the integer 3 to the controller. Please help! Thanks!
$.ajax({
type: "GET",
url: "http://localhost:8086/xxx",
crossDomain: true,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
var testData = '';
$.each(data, function (key, value) {
testData += '<tr>';
var id = value.testDataID.toString();
testData += '<td style="cursor:pointer">' + '<a href="http://localhost:8087/xxx/yyy?param="+{id}>' + value.serialNumber + '</a>' + '</td>';
testData += '<td>' + value.testDataID + '</td>';
testData += '<td>' + value.sN + '</td>';
testData += '</tr>';
});
$('#testDataTable').append(testData);
}
});

How to show Total No. of post In blogger with ajax get method

I want to show total no. of post in blogger with ajax get method bellow is my code.
<div id='show'></div>
<script type="text/javascript">
$.ajax({
url: "https://techtovillage.blogspot.com/feeds/posts/default?orderby=published&max-results=10&start-index=2&alt=json-in-script",
type: "get",
dataType: "jsonp",
success: function (data) {
var Posts = '<div class="hindipathtestapp">';
for (var i = 0; i < data.feed.entry.length; i++) {
var totalposts = json.feed.openSearch$totalResults.$t;
var posttitle = data.feed.entry[i].title.$t;
var postcontent = data.feed.entry[i].content.$t;
Posts += "<div class='testapp'><h1>" + totalposts + "</h1><h3>" + posttitle + "</h3><div class='content'>" + postcontent + "</div></div>";
}
Posts += "</div>"
document.getElementById('show').innerHTML = Posts;
}
});
</script>
Try this
<div id='show'></div>
<script type="text/javascript">
$.ajax({
url: "https://techtovillage.blogspot.com/feeds/posts/default?alt=json-in-script",
type: "get",
dataType: "jsonp",
success: function (data) {
var totalposts = data.feed.openSearch$totalResults.$t;
document.getElementById('show').innerHTML = "<div class='totalposts'>" + totalposts + "</div>";
}
});
</script>

Calculate the json's value then show in html

I have a json array:
[{"value": "1"},{"value": "2"},{"value": "3"},{"value": "4"},{"value": "5"},{"value": "6"}]
My code is :
$.ajax({
url: "120.58.243.11:8080/needCal/myJson.json",
dataType: 'json',
success: function(data) {
var items = [];
$.each(data, function(key, value) {
items.push("<tr>");
items.push("<td id=''" + key + "''>" + value.value+ < /td>");
items.push("<td id=''" + key + "''>" + total of values + < /td>");
items.push("</tr>");
});
}
});
I want to calculate the values, how to do with that?
$.ajax({
url: "120.58.243.11:8080/needCal/myJson.json",
dataType:'json',
success: function(data){
var items = [];
var totalOfValue = 0;
$.each(data,function(key,value){
totalOfValue = totalOfValue + parseInt(value.value);
});
$.each(data,function(key,value){
items.push("<tr>");
items.push("<td id='" +key+ "'>" + value.value+</td>");
items.push("<td id='" +key+ "'>" + totalOfValue +</td>");
items.push("</tr>");
});
}
}
);
A simple way is to use the reduce method:
Something like this:
var total = arr.reduce(function(t, v) {
return t += Number(v.value);
}, 0);

How to maintain the dynamic generated textboxes after postback

I am generating dynamic html textboxes in my aspx page. I add a button for do some functionality. now whenever I click on my button it post back and all the dynamic generated textboxes go from my form for this I have to add it again. But I want to maintain these textboxes after postback. Here is the code how i am generating textboxes
<script type="text/javascript">
var textid = null;
var textid1 = null;
$(document).ready(function () {
setupAutoComplete(textid);
setupAutoComplete1(textid1);
var counter = 2;
$("#addButton").click(function () {
if (counter > 5) {
alert("Limit Exceeds");
return false;
}
var newTextBoxDiv = $(document.createElement('div'))
.attr("id", 'TextBoxDiv' + counter);
textid = "textbox" + counter;
textid1 = "textbox" + counter+1;
// newTextBoxDiv.after().html('<label>Textbox #' + counter + ' : </label>' +
// '<input type="text" name="textbox' + counter +
// '" id="textbox' + counter + '" value="" class="auto">');
newTextBoxDiv.after().html('<div class="fields-left"><label> Leaving from</label><input type="text" name="textbox' + counter + '" id="textbox' + counter + '" class="auto"/> </div><div class="fields-right"> <label> Going to</label> <input type="text" name="textbox' + counter + '" id="textbox' + counter + 1 + '" class="auto"/> </div>');
newTextBoxDiv.appendTo("#TextBoxesGroup");
setupAutoComplete(textid);
setupAutoComplete1(textid1);
counter++;
});
$("#removeButton").click(function () {
if (counter == 1) {
alert("No more textbox to remove");
return false;
}
counter--;
$("#TextBoxDiv" + counter).remove();
});
});
var setupAutoComplete= function SearchText2(textid) {
$('.auto').autocomplete({
source: function (request, response) {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Home.aspx/GetAutoCompleteData",
data: "{'code':'" + document.getElementById(textid).value + "'}",
dataType: "json",
success: function (data) {
response(data.d);
},
error: function (result) {
alert("Error");
}
});
}
});
}
var setupAutoComplete1 = function SearchText3(textid1) {
$('.auto').autocomplete({
source: function (request, response) {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Home.aspx/GetAutoCompleteData",
data: "{'code':'" + document.getElementById(textid1).value + "'}",
dataType: "json",
success: function (data) {
response(data.d);
},
error: function (result) {
alert("Error");
}
});
}
});
}
</script>
now in this code I am generating maximun 10 textboxes two in each row and after that I write some code for json autocomplete for each dynamic generated textbox. Now problem is this when i click on my button after that its postback and all the textboxes and its values goes away. I am unable to maintain these textboxes. Can anybody tell me how can we maintain the dymanic generated textboxes after postback?
Thanks

I'm having trouble parsing response from $.ajax

I'm trying to modify some old code of mine to include comments for photos. The photos are acquired using $.ajax and the response is in html.
My modification is to get the comments as a json object and then parse the previously attained html inserting the comments where appropriate. Here is the [latest incarnation] of my code (I've tried many different alternatives)
$.ajax({
type:"POST",
url:"<?php echo $_SERVER['PHP_SELF']; ?>",
data:"ajax_loadbigthumbs=true&section=<?php echo $_GET['section']; ?>",
dataType : "html",
success: function(html){
jhtml = $(html);
$.getJSON( "/commentsjson.php?getcomments=true&section=<?php echo $_GET['section']; ?>", function( data ) {
$.each(data,function(i,item){
//alert(item.comment); <--- this works so I know the data is coming back correctly
console.log(jhtml.find('#comments_' + item.photoid).attr("id")); // this shows 'undefined'
jhtml.find('#comments_' + item.photoid).css("display","block").html("<p>" + item.name + " said " + item.comment + "</p>");
});
$("#largethumbscontainer").append(jhtml);
});
}
});
But this isn't working. The console.log line (as a test) returns 'undefined' and the following line (jhtml.find) has not found anything to modify.
var $comment = $('#comments_' + item.photoid, jhtml);
if ($comment.length)
$comment.css("display","block").html("<p>" + item.name + " said " + item.comment + "</p>");
I fixed it, buy appending the html to an already existing element, and then appending the comments to that element....
$.ajax({
type:"POST",
url:"<?php
echo htmlspecialchars($_SERVER["PHP_SELF"], ENT_QUOTES, "utf-8");
?>",
data:"ajax_loadbigthumbs=true&section=<?php echo $_GET['section']; ?>",
dataType : "html",
success: function(html){
jhtml = $(html);
$("#largethumbscontainer").append(jhtml);
largethumbs = $("#largethumbscontainer");
$.getJSON( "/commentsjson.php?getcomments=true&section=<?php
echo $_GET['section'];
?>", function( data ) {
$.each(data,function(i,item){
largethumbs
.find('#comments_' + item.photoid)
.css('display','block')
.append('<p>' +
item.name +
' said: <span style="font-style:italic;">' +
item.comment +
'</span></p>');
});
});
}
});