I have been using this script for a while and suddenly it just stopped working and returns the error message. I cannot find any errors with the code and the php page echoes valid json. Please if anyone can find an error or something wrong with this code let me know. I am using the same script other places just fine.
Go to:
http://ab-mobile-apps.com/app/grotto/index.html
then click random drink to see live. Clicking the error message will call the function again.
Thanks ahead.
function loadData() {
var output = $('#output');
var drinkImageOutput = $('#drinkImage');
var drinkIngredientOutput = $('#drinkIngredient');
var drinkNameOutput = $('#drinkName');
output.text('');
$.ajax({
url: 'http://ab-mobile-apps.com/grototest/index.php',
dataType: 'jsonp',
jsonp: 'jsoncallback',
timeout: 10000,
success: function(data, status){
$.each(data, function(i,item){
var landmark =
'<div id="drinkImage"><img src="' + drinkImg + '" width="15%" /></div>' +
'<div id="drinkName">' + drinkName + '</div>' +
'<div id="dringIngredient">' + dringIngredient + '</div>';
output.append(landmark);
});
},
error: function(){
output.text('There was an error loading the data.');
}
});
}
Your request returns JSON not JSONP.
Try:
$.ajax({
url: 'http://ab-mobile-apps.com/grototest/index.php',
dataType: 'json',
timeout: 10000,
success: function(data, status){
$.each(data, function(i,item){
var landmark =
'<div id="drinkImage"><img src="' + item.url + '" width="15%" /></div>' +
'<div id="drinkName">' + item.sname + '</div>' +
'<div id="dringIngredient">' + item.ingredients + '</div>';
output.append(landmark);
});
},
error: function(){
output.text('There was an error loading the data.');
}
});
Related
I have been trying all the help available on different forums but now give up and posting my question here. Trying to populate dropdown using ajax call. Getting the data in json format successfully. But don't know to fill dropdown. code below:
Controller:
function getRegion()
{
$this->load->model('Settings_model');
$title = $this->input->get('title');
$result = array("region" => $this->Settings_model->getSelectedRegion($title));
echo json_encode($result);
}
View:
$(document).on('change', '#campaignSel', function() {
changecampaign();
});
function changecampaign(){
var title= "New Normal";//$('#campaignSel option:selected').text();//$('#campaignSel').text();
$regionSel = $("#regionSel");
$.ajax({
type:"GET",
url: "<?php echo base_url('Pricecomparison/getRegion'); ?>",
data:{ "title":"New Normal"},
datatype: "json",
success: function(result){
var appenddata;
$.each(result, function (key, value) {
appenddata += "<option value = '" + value.id + " '>" + value.region + " </option>";
});
$('#regionSel').html(appenddata);
},
error: function(xhr, textStatus, error){
console.log(xhr.statusText);
console.log(textStatus);
console.log(error);
}
});
}
Response: {"region":[{"id":"1","region":"109 FEATHERSTON ST (ELEC)\r\n"},{"id":"2","region":"ASHBURTON/MID CANTERBURY
ELEC"}]}
I want to fill dropdown with Regions.
you need to loop the region like this $.each(result.region, function (key, value) { instead of result
Update1:
added if condition if ($.isArray(result.region)){ ... }
result = {"region":[{"id":"1","region":"109 FEATHERSTON ST (ELEC)\r\n"},{"id":"2","region":"ASHBURTON/MID CANTERBURY ELEC"}]}
var appenddata='';
if ($.isArray(result.region)){
$.each(result.region, function (key, value) {
appenddata += "<option value = '" + value.id + " '>" + value.region + " </option>";
console.log(appenddata);
});
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Finally I resolved the issues in my code. I like to share the changes I made to my code below:
added:
contentType: "application/json",
and
parsedobj = JSON.parse(result)
so the final ajax code is:
$.ajax({
type:"GET",
url: "<?php echo base_url('Pricecomparison/getRegion'); ?>",
data:{ "title":title},
contentType: "application/json",
datatype: "json",
success: function(result){
parsedobj = JSON.parse(result)
var appenddata='';
$.each(parsedobj.region, function(index, value)
{
appenddata += "<option value = '" + index + "'>" + value.region + " </option>";
});
$('#regionSel').html(appenddata);
},
error: function(xhr, textStatus, error){
console.log(xhr.statusText);
console.log(textStatus);
console.log(error);
}
});
All that I am trying is to iterate the data I have in the database using the Jquery each method, but couldn't figure when I am losing it all. Can someone look through this piece of code and tell me what I am doing wrong ?
$(function(){
var output = $("#folderOne");
$.ajax({
url: 'http://......',
dataType: 'json',
// data: {action: 'output'},
success: function(data, status){
console.log(data);
$.each(data, function(index, value) {
if (value.f_id == 1) {
var cards = "<div class='thumb'>" + value.f_id +
"</div><br>" + "<p>" + value.title + "</p> ";
}
output.append(cards);
});
},
I'd like to send series["id"+i] to write.php. (This code is in another ajax request.)
Any idea?
series = new Object();
$(xml_node).find("Series").each(function (i) {
series["id" + i] = $(this).find("seriesid").text();
series["name" + i] = $(this).find("SeriesName").text();
series["banner" + i] = $(this).find("banner").text();
table += '<tr<td>' + series["banner" + i] + '</td>' + '<td>' + series["name" + i] +
'</td>' + '<td>' + '<button>Add show</button>' + '</td>' + '</tr>';
});
$('button').click(function addseries() {
$.ajax({
type: "POST",
url: 'write.php',
data: series["id" + i],
success: function (data) {
console.log(data);
}
});
});
...'<button data-id="' + i +'">Add show</button>'..
$('button').click(function addseries(e){
var i = $(e.currentTarget).attr('data-id');
$.ajax({
type: "POST",
url: 'write.php',
data: i,
success: function(data) {
console.log(data);
}
});
UPDATE:
data: {id: i}
or
data: 'id=' + i
You can get the event of what triggered the ajax call,
for example
$('button').click(function addseries(e){
console.log($(e.target));
}
where $(e.target) is the jQuery object that triggered the call.
You can also use $(this)(referring to the jQuery object that called the function).
so in our example,
$('button').click(function addseries(e){
console.log( $(this).attr('id') );
});
I have seen some other posts in Stackoverflow which were related. Tried that code but it did not work out for me.
actually i have a database with 2 images of an actress in my MYSQL database. I am generating JSON data using PHP and it works fine.
Link for JSON data
I am trying to parse it with Javascript as shown in this fiddle
Direct Parsing Fiddle Link
var json = [{
"id": "1",
"url": "http:\/\/i.imgur.com\/J8yqh3y.jpg"
}, {
"id": "2",
"url": "http:\/\/i.imgur.com\/WNx9i2c.jpg"
}];
var nazriya = json;
$.each(nazriya, function (index, nazriya_nazim) {
$('#url-list').append('<li>' +
'<h4>' + nazriya_nazim.url + '</h4>' +
'</li>');
});
and it works fine.
But if i try to parse it from my PHP file located in my domain. It does not display anything. It shows blank page.
FIDDLE Link : Parsing JSON data on PHP File
type: "POST",
dataType: 'json',
url: "http://chipap.com/apps/nazriya_nazim/test1.php",
success: function (data) {
alert("1");
//var obj = jQuery.parseJSON(idata);
var json = JSON.parse(data);
$.each(json, function (index, nazriya) {
$('#url-list').append('<li>' +
'<h4>' + nazriya.url + '</h4>' +
'</li>');
});
}
I did not write all these code on my own. Browsed Stack and found solutions. But stuck up at the last step (parsing from a PHP file located in my server).
As said by #DaGLiMiOuX tried it in a separate HTML document.
<head>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script>
$.ajax({
type: "POST",
dataType: 'jsonp',
url: "http://chipap.com/apps/nazriya_nazim/test1.php",
success: function (data) {
alert("1");
var json = data;
$.each(data, function (index, nazriya) {
$('#url-list').append('<li>' +
'<h4>' + nazriya.url + '</h4>' +
'</li>');
});
},
error: function(jqXHR, status, errorText) {
alert('Status code: ' + jqXHR.status +
'\nStatus text: ' + status + '\nError thrown: ' + errorText);
}
});
</script>
</head>
<body>
<ul id="url-list"></ul>
</body>
Now also it shows the same error.
In your client side specified the jsonpcallback as below
$.ajax({
type: "POST",
dataType: 'jsonp',
url: "http://chipap.com/apps/nazriya_nazim/test1.php",
jsonpCallback: function(data){
alert('generate a specified jsonp callback');
return "jsonpCall";
},
success: function (data) {
alert("1");
var json = data;
$.each(data, function (index, nazriya) {
$('#url-list').append('<li>' +
'<h4>' + nazriya.url + '</h4>' +
'</li>');
});
},
error: function(jqXHR, status, errorText) {
alert('Status code: ' + jqXHR.status +
'\nStatus text: ' + status + '\nError thrown: ' + errorText);
}
});
In http://chipap.com/apps/nazriya_nazim/test1.php
<?php
$callback = $_GET["callback"]; // return "jsonpCall" that was specified in jsonpCallback ajax with jsonp
$json = '[{"id":"1","url":"http:\/\/i.imgur.com\/J8yqh3y.jpg"},{"id":"2","url":"http:\/\/i.imgur.com\/WNx9i2c.jpg"}]' ;
echo $callback.'('. $json.')' ;
?>
You can understand much better about jsonp at at http://en.wikipedia.org/wiki/JSONP
http://jsfiddle.net/channainfo/wn5bz/
1) that's just an extract of a code, not a compiling function. The complete code would be
$.ajax({
type: "POST",
dataType: 'json',
url: "http://chipap.com/apps/nazriya_nazim/test1.php",
success: function (obj) {
alert("1");
$.each(obj, function (index, nazriya) {
$('#url-list').append('<li>' +
'<h4>' + nazriya.url + '</h4>' +
'</li>');
});
}
});
2) you need to import jQuery (you don't do it in the fiddle)
XMLHttpRequest cannot load
http://chipap.com/apps/nazriya_nazim/test1.php. Origin
http://fiddle.jshell.net is not allowed by
Access-Control-Allow-Origin.
You have to handle ALWAYS posible errors.
Check this demo.
This should work, but you got Access-Control-Allow-Origin error. This is caused because your dataType was incorrect. Try this configuration for your ajax call:
$.ajax({
type: "POST",
dataType: 'jsonp',
url: "http://chipap.com/apps/nazriya_nazim/test1.php",
success: function (data) {
alert("1");
var json = data;
$.each(data, function (index, nazriya) {
$('#url-list').append('<li>' +
'<h4>' + nazriya.url + '</h4>' +
'</li>');
});
},
error: function(jqXHR, status, errorText) {
alert('Status code: ' + jqXHR.status +
'\nStatus text: ' + status + '\nError thrown: ' + errorText);
}
});
NOTE: You get in the demo an alert as if it were an error, but your status code is 200 (check status codes). Try it in your project. Maybe JsFiddle it's not allowing to return data from external servers.
I need to post data to another server using jquery.
Here is the code i am using
$.ajax({
url:"https://www.thewiseagent.com:443/secure/webcontactAllFields.asp",
type:'POST',
data:"ID=" + $ID
+ "&Source=" + $Source
+ "¬ifyCc=" + $notifyCc
+ "¬ifyBcc=" + $notifyBcc
+ "&noMail=" + $noMail
+ "&CFirst=" + $first
+ "&CLast=" + $last
+ "&Phone=" + $Phone
+ "&Fax=" + $Fax
+ "&CEmail=" + $CEmail
+ "&Message=" + $message,
success: function() {
//window.location.href = "http://www.petlooza.com";
}
});
i got error (302 object moved) in case of firefox/chorme although data is inserting.. but in case of IE data is not entering in external database. In IE i got a Access denied error.
Can anyone have alternative?
I have tried with json and jsonp still same error.
$.ajax({
type: "POST",
url: "https://www.thewiseagent.com:443/secure/webcontactAllFields.asp",
data: dataString,
dataType: "jsonp",
success: function(data) {
}
});
If you want to use $.ajax() and make a request to another domain you must set crossDomain option to true as stated in the documentation
$.ajax({
url:"https://www.thewiseagent.com:443/secure/webcontactAllFields.asp",
type:'POST',
crossDomain: true,
data:"ID="+$ID+"&Source="+$Source+"¬ifyCc="+$notifyCc+"¬ifyBcc="+$notifyBcc+"&noMail="+$noMail+"&CFirst="+$first+"&CLast="+$last+"&Phone="+$Phone+"&Fax="+$Fax+"&CEmail="+$CEmail+"&Message="+$message,
success: function() {
//window.location.href = "http://www.petlooza.com";
}
});
You could make an AJAX request to a php script on your own server, which then gets the information from the other server and returns it to you jQuery. I can't think of any other way at the moment.
You have a crossdomain problem. Try using jsonp:
$.ajax({
url:"https://www.thewiseagent.com:443/secure/webcontactAllFields.asp",
type:'POST',
dataType: "jsonp",
data:"ID="+$ID+"&Source="+$Source+"¬ifyCc="+$notifyCc+"¬ifyBcc="+$notifyBcc+"&noMail="+$noMail+"&CFirst="+$first+"&CLast="+$last+"&Phone="+$Phone+"&Fax="+$Fax+"&CEmail="+$CEmail+"&Message="+$message,
success: function(data) {
//window.location.href = "http://www.petlooza.com";
}
});