getjson query is not working - javascript

Json page is : http://freegeoip.net/json/59.92.78.49
and my code is
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$.getJSON("http://localhost/02/t.php",function(result){
alert(result.ip);
});
});
});
</script>
this simple code is not working :(

Seem like freegeoip.net support jsonp,you can do:
$('button').click(function () {
$.ajax({
url: "http://freegeoip.net/json/59.92.78.49",
dataType: "jsonp",
success: function (result) {
alert(result.ip); // server response
}
});
});
Fiddle Demo

Related

$.Ajax is not a function during combo box selection change

Here is the Code. I've already researched about it and so far, haven't found the right solution. Any help would be much appreciated. Thanks.
<script type="text/javascript" src="{%static 'javascript/jquery-3.1.1.js'%}"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#itemid").change(function(){
var itemid = $(this).val();
var func_url = '/get_itemdetails/' + itemid +'/';
$.ajax({
type:"GET",
url: func_url,
dataType:"json",
success: function(data){
console.log(data);
}
});
});
});
</script>
Below is the latest script header for Google's hosted CDN which allows for the $.ajax Function:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#itemid").change(function(){
var itemid = $(this).val();
var func_url = '/get_itemdetails/' + itemid +'/';
$.ajax({
type:"GET",
url: func_url,
dataType:"json",
success: function(data){
console.log(data);
}
});
});
});
</script>

How to Simply Ajax Script (Removing duplicate code)?

I want to simplify the following script, and create a reuseable funciton,
I use it with:
<span id="test"></span>
<span id="abc"></span>
<span id="123"></span>
here it is now:
<script>
$(document).ready(function(){
var callAjax = function(){
$.ajax({
method:'get',
url:'abctest.php',
success:function(data){
$("#test").html(data);
}
});
}
setInterval(callAjax,1000);
});
$(document).ready(function(){
var callAjax = function(){
$.ajax({
method:'get',
url:'getabc.php',
success:function(data){
$("#abc").html(data);
}
});
}
setInterval(callAjax,1000);
});
$(document).ready(function(){
var callAjax = function(){
$.ajax({
method:'get',
url:'123.php',
success:function(data){
$("#123").html(data);
}
});
}
setInterval(callAjax,1000);
});
</script>
as you can see, all I need is to change the url and #, so I create a function:
function good(url,tag){
var callAjax = function(){
$.ajax({
method:'get',
url:'url.php',
success:function(data){
$("#tag").html(data);
}
});
}
setInterval(callAjax,1000);
}
and rewrite the script to:
<script>
$(document).ready(good(abctest,test));
$(document).ready(good(getabc,abc));
$(document).ready(good(123,123));
</script>
looks much better. but seems not so easy.
it not working. how to solve this problem?
Your almost there. You need to manually concatenate the strings like
function good(url,tag){
var callAjax = function(){
$.ajax({
method:'get',
url:url+'.php',
success:function(data){
$("#"+tag).html(data);
}
});
}
setInterval(callAjax,1000);
}
Javascript is not like php where variables can be evaluated between double quotes.
Also, you need to pass your parameters in as strings
$(document).ready(function(){
good('abctest','test');
good('getabc','abc');
good('123','123');
});

jQuery Ajax File fetching issue

I am trying to fetch the files contents using ajax but it is not working ? I dont know why, even though I have written the same code written on w3schools.com examples.
$().ready(function(){
$("button").click(function(){
$.ajax({url:"Sample.txt",
success:function(result)
{
alert(result);
//$("#changedText").text(result);
}
});
});
});
You replace your code with this code....
$(function(){
$("#btn_id").click(function(){
$.ajax({
url:"your_url.php",
type: "GET",
success: function(rt) {
alert(rt);
}
});
});
});

How access data in JavaScript arrays using $.ajax

This is a script when the script is executed I want to load the data into the div of id #trial. The script is an external array with three names the second I am trying to call is 'sarah'. Whats wrong?
<script>
$(document).ready(function(){
$("#trial").click(function(){
var attempt=$.ajax({
url: "names.js",
dataType: "script"
});
var msg=names[1];
attempt.done(function( msg ) {
$( "#trial" ).html( msg );
});
});
});
</script>
Change your code to this:
<script>
$(document).ready(function(){
$("#trial").click(function(){
$.ajax({
url: "names.js",
dataType: "script",
success: function(res){
//todo something with res
}
});
});
});
</script>

import the all xml attribute value with click method

i have Something this type of xml with huge of data:-
<root>
<child1 id="4331" value="twenty-twenty">
<child2 id="4332" value="India">
<child4 id="4334" value="Mumbai Indian"></chlild4>
<child5 id="4335" value="Rajshthan Royal"></child5>
</child2>
<child3 id="4333" value="Pakishtan">
<child6 id="4336" value="Arngabad"></child6>
<child7 id="4337" value="Punjab"></child7>
</child3>
</child1>
</root>
i tried this
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("button").click(function() {
});
});
$(function() {
$.ajax({
type: "GET",
url: "atry.xml",
dataType: "xml",
success: function(xml) {
data = xml;
$(xml).find('*').each(function(){
console.log($(this).attr('id'));
console.log($(this).attr('value'));
});//close $.each(
}
}); //close click(
});
</script>
</head>
<body>
<!-- we will add our HTML content here -->
<button>Link</button>
</body>
</html>
i am try this but no output display...
import the all child attribute using jquery with click event...
thanks
You can try this code with jquery:
var xml = '<root><child1 id="4331" value="twenty-twenty"><child2 id="4332" value="India"><child4 id="4334" value="Mumbai Indian"></chlild4><child5 id="4335" value="Rajshthan Royal"></child5></child2><child3 id="4333" value="Pakishtan"><child6 id="4336" value="Arngabad"></child6><child7 id="4337" value="Punjab"></child7></child3></child1></root>';
var $root = $(xml);
$root.find('*').each(function(){
console.log($(this).attr('id'));
console.log($(this).attr('value'));
});
You may try like this
$(document).ready(function() {
$("button").click(function() {
$.ajax({
type: "GET",
url: "atry.xml",
dataType: "xml",
success: function(xml) {
data = xml;
$(xml).find('*').each(function(){
console.log($(this).attr('id'));
console.log($(this).attr('value'));
});//close $.each
}
}); //cloase ajax
}); //close button click
}); //close ready

Categories