This question already has answers here:
Ways to circumvent the same-origin policy
(8 answers)
Closed 8 years ago.
I'm trying to get a time data from json with the code bellow, but i get a error instead of the data, what can be happening?
$.ajax({
type: "POST",
url: "http://www.previsaodotempo.org/api.php?city=Rio+De+Janeiro",
success: function(data) {
alert()
data.location;
$("div").html(data);
},
error: function(data) {
$("div").html('can not get the json');
}
});
Here is a fiddle: http://jsfiddle.net/jodgjqwf/
Is http://www.previsaodotempo.org/api.php?city=Rio+De+Janeiro expecting you to POST to it? I just hit the url and it returned the JSON to me.
Change "POST" to "GET".
$.ajax({
type: "GET",
url: "http://www.previsaodotempo.org/api.php?city=Rio+De+Janeiro",
success: function(data) {
$("div").html(data);
},
error: function(data) {
$("div").html('can not get the json');
}
});
Related
This question already has answers here:
How can I access object properties containing special characters?
(2 answers)
Closed 1 year ago.
Im trying to build a ajax call using some $post data that require the score separator and always face an issue of wrong syntax.
$.ajax({
url: "register.php",
type: "post",
cache: false,
dataType: 'json',
data: {
register-username: name,
register-email: email,
},
success: function(response) {
},
error: function(){
}
});
i wonder how i can use those register-username and register-email without having a syntax error. Changing those for no separator is not an option.
Use quotes
data: {
"register-username": name,
"register-email": email,
},
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 8 years ago.
I have a PHP script that takes a query string returns and JSON object. I am trying to make an AJAX call that will return this JSON object so I can use it in jQuery autocomplete. Here is my AJAX call so far
$(document).ready(function(){
$("#searchBox").keyup(function(){
var search_result = $.ajax({
url:"/mm5/service/product_lookup_json.php",
type: "GET",
dataType: "json",
jsonp: false,
data: "query=" + $("#searchBox").val(),
success: function(data){
return data;
}
});
console.log(search_result);
});
});
This sends the result of the AJAX call to the console ( a javascript object). I can see a JSON in that object. I would think all I have to do to access the data I want is this
console.log(search_result.responseJSON);
But this just gives me undefined. What am I doing wrong here?
That is not the way to do it because it is an async call so the data will not be returned by the ajax call. Try this
$(document).ready(function(){
var search_result;
$("#searchBox").keyup(function(){
$.ajax({
url:"/mm5/service/product_lookup_json.php",
type: "GET",
dataType: "json",
jsonp: false,
data: "query=" + $("#searchBox").val(),
success: function(data){
search_result = data;
console.log(search_result);
}
});
});
});
You need to wait for the AJAX call to complete in order to access the data it returns.
$(document).ready(function(){
$("#searchBox").keyup(function(){
$.ajax({
url:"/mm5/service/product_lookup_json.php",
type: "GET",
dataType: "json",
jsonp: false,
data: "query=" + $("#searchBox").val(),
success: function(data){
//data is available at this points
console.log(data);
}
});
});
});
This question already has answers here:
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
(7 answers)
Closed 8 years ago.
Hello I have a problem storing my json in a global variable, what I want is to do first all my ajax request then store each of the returned data to a global variable, but it seems it's not working correctly? Can Any help me to my problem? Thanks. :)
var series;
function columnChart(container)
{
url = base_url+"/codeigniter/index.php/AssistanceMonitoringModule/assistanceMonitoring/getSeries";
$.ajax(
{
type: "GET",
url: url,
success: function(data){
series = data;
},
dataType: "json",
error: function (xhr, ajaxOptions, thrownError) {
alert("XHR:"+xhr.status+"Error:"+thrownError);
}
});
callColumnChart(container,series);
}
You are using the variable too soon. The Ajax request will not be completed when you call callColumnChart. Move it into the ajax callback.
$.ajax(
{
type: "GET",
url: url,
success: function(data){
series = data;
callColumnChart(container,series);
},
dataType: "json",
error: function (xhr, ajaxOptions, thrownError) {
alert("XHR:"+xhr.status+"Error:"+thrownError);
}
});
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 7 years ago.
I have this javascript:
$ajax = $.ajax({
type: 'GET',
url: 'DBConnect.php',
data: '',
dataType: 'json',
success: function(data) {},
error:function (xhr, ajaxOptions, thrownError) {
dir(thrownError);
dir(xhr);
dir(ajaxOptions);
}
});
console.dir($ajax);
console.dir($ajax.responseJSON);
console.dir($ajax) shows it has a property named responseJSON, but when I try to access it with $ajax.responseJSON it returns undefined:
Well, of course it's undefined, because at the moment when you run console at last lines of your code, response hasn't yet came from the server.
$.ajax returns promise, which you can use to attach done() and fail() callbacks, where you can use all the properties that you see. And you have actually used callback error and success, and that's where you can run code and other functions that rely on data in the response.
You can use this trick to get the response out:
jQuery.when(
jQuery.getJSON('DBConnect.php')
).done( function(json) {
console.log(json);
});
It's late but hopefully will help others.
The response, is the "data", in success... so you can access to that writing data[0], data[1], inside the success.
For example:
success: function(data) {
alert(data[0]);
},
If you want this response, out of the success, you can set a variable outside, and try this:
success: function(data) {
myVar = data;
},
Hope, this help.
For those who don't really mind if it's synchronous, like I was, you can do this:
$('#submit').click(function (event) {
event.preventDefault();
var data = $.ajax({
type: 'POST',
url: '/form',
async: false,
dataType: "json",
data: $(form).serialize(),
success: function (data) {
return data;
},
error: function (xhr, type, exception) {
// Do your thing
}
});
if(data.status === 200)
{
$('#container').html(data.responseJSON.the_key_you_want);
}
});
It runs through the motions, waits for a response from the Ajax call and then processes it afterwards if the status == 200, and inside the error function if something triggered an error.
Edit the options to match your situation. Happy coding :)
This question already has answers here:
jQuery XML error ' No 'Access-Control-Allow-Origin' header is present on the requested resource.'
(2 answers)
Closed 9 years ago.
I m try to write ajax get by using jquery in jsp file. But it always gave error message. I could`nt find the error in my method.
This is my ajax function. Please help me.
<script type="text/javascript">
$.ajax({
url: "http://crowderia.cloudapp.net/ichainwsDev/api/rest/json/product.list",
type: "GET",
data: '{"partyid":4}',
contentType: 'application/json; charset=UTF-8',
dataType: 'json',
success: function(msg){
alert("success : "+data);
},
error:function(msg){
alert("failure");
}
});
</script>
Error is :
XML HttpRequest cannot load http://crowderia.cloudapp.net/ichainwsDev/api/rest/json/product.list.
No 'Access-Control-Allow-Orgin' header is present on the requested resource. Orgin 'http:// localhost:4040' is therefore not allowed access
Try your data
data: {"partyid":4},
in place of
data: '{"partyid":4}',
And try your success callback like,
success: function(msg){
alert("success : "+msg);// use msg not data
},
Full Code
$(function(){
$.ajax({
url: "http://crowderia.cloudapp.net/ichainwsDev/api/rest/json/product.list",
type: "GET",
data: {"partyid":4},
contentType: 'application/json; charset=UTF-8',
dataType: 'json',
success: function(msg){
alert("success : "+msg);
},
error:function(msg){
alert("failure");
}
});
});
You cloud app url works