I'm trying to make ajax call to display current weather info however, only 'icon' is not displaying. All the other items are working just fine.
function APIcalls() {
// URL to make API calls for current forecast
let currentForeCastUrl = "https://api.openweathermap.org/data/2.5/weather?q=" +
city + "&appid=0fb779217270dc1943ca287687bf9395";
$.ajax({
url: currentForeCastUrl,
method: "GET",
}).then(function (response) {
let temp = Math.round(((response.main.temp - 273.15) * 9 / 5 + 32));
$("#icon").attr("src", "http://openweathermap.org/img/w/" + response.weather[0].icon + ".png");
});
}
Related
I am using the following ajax function to post to a php script. (url_one.php)
How could I post the same data (myvar1, myvar2) to a second php script (ajaxURL2) at the same time ? Is there a way to do this ?
function ajax_function() {
var myvar1 = $('myvar1').val();
var myvar2 = $('.myvar2:checked').val();
var ajaxURL = window.location.protocol + "//" + window.location.host + "/"+"url_one.php";
var ajaxURL2 = window.location.protocol + "//" + window.location.host + "/"+"url_two.php"; // <-- to 2nd url simultaneously
$.ajax({
async: false,
type: "POST",
url: ajaxURL,
timeout : 8000,
data: {functionname: 'my_php_function', arguments: [myvar1, myvar2]},
success: function (data) {
if( !('error' in data) ) {
objDiv.scrollTop = objDiv.scrollHeight;
}},
});
}
The ajax function is being called every five seconds using setTimeout .. (and never misses a beat..)
setTimeout(function(){
ajax_function() ;
//... something here ?
}, 5000);
Should I be looking at this part, to post to more than one script at the same time? or be looking into a php side solution. Many thanks in advance.
I have a problem with uploading a blob using ajax. I have tried many options and they are not working.
I must say that the same variables (albeit with different names) work when using php in the standard sense but not when using ajax.
Please help
The ajax code is:
$(function() {
$("#upload").click(function() {
// validate and process form here
var username = $("input#username").val();
var title = $("input#title").val();
var image = $("#image").get(0).files.item(0);
var information = tinymce.get('blogcontent').getContent();
var dt = new Date();
// variable for blog date and time
var dateandtime = dt.toLocaleString();
var dataString = 'username=' + username + '&title=' + title + '&image=' + image + '&information=' + information + '&dateandtime=' + dateandtime;
$.ajax({
type: "POST",
url: "functions/insertblogpost.php",
data: dataString,
success: function() {
$('#writeblog').html("<div id='message'></div>");
$('#message').html("<h2>User account created!</h2>")
.append("<p>Please go back to login.</p>")
.hide()
.fadeIn(1000, function() {
$('#message').append("<a href='../../Mobileapptemplate.php'>Back</a>");
});
}
});
return false;
});
});
And the php script is:
$connection = mysqli_connect($dbserver, $dbusername, $dbpassword, $database);
$username = $_POST[ 'username' ];
$blogTitle = $_POST["title"];
$blogContent = $_POST["information"];
$blogpicturename = $_FILES["image"]["name"];
$blogpicdata = mysqli_real_escape_string( $connection, file_get_contents($_FILES["image"]["tmp_name"]));
$blogpictype = $_FILES['image']['type'];
$dateAndTime = $_POST["dateandtime"];
$result = "INSERT INTO ct5006ho_users.$username ( postnumber, user, title,
picturename, picture, blogpost, dateandtime ) VALUES ( '', '$username', '$blogTitle','$blogpicturename',
'$blogpicdata','$blogContent', '$dateAndTime');";
//if (
mysqli_query($connection, $result);
The connection IS established fine and all other data uploads to the phpmyadmin created database. I have omitted those details from the code.
Here is the answer:
$(function() {
$("#upload").click(function() {
// validate and process form here
tinyMCE.triggerSave();
var form = $('form')[0]; //
var formData = new FormData(form);
$.ajax({
type: "POST",
url: "functions/insertblogpost.php",
data: formData, // Data sent to server, a se
contentType: false,
cache: false, // To unable request pages to be cached
processData:false,
success: function() { ....................
If anybody has a similar issue please use this experience to help you.
Note - tinyMCE.triggerSave(); - sends the tinyMCE data. and this was my original issue with the - data: formData,
Ah well, shoot me down for asking!
Here is code:
loader:function(param,success,error){
$.ajax({
//url: 'http://localhost/mvcController?assetid=1&dataformat=attribute',
url: assetsMVCService.execute("mvcController", { assetId: 1, dataFormat: 'attribute' }),
dataType: 'text',
type: 'post',
success: function (data) {
alert('ok');
},
error: function () {
error.apply(this, arguments);
}
});
assetsMVCService is my wrapper for Angular service. Unfortunately, a product that I am implementing forces me to use AJAX call to get data. Normally, I would use services to get data and then scope for data binding. Is it possible to use a service assigned to the url property in the code above? Interesting enough, I am hitting server with the code above. But something gets wrong on the server.
Thanks
Yes. You could do something like this:
app.service('MVC', function($http) {
var root = 'http://localhost/mvcController';
var queryParams = '?assetid=1&dataformat=attribute';
this.get = function(num) {
return $http.get(root + '/' + num + queryParams);
};
// or if you want to pass the query params in
this.execute = function(assetId, dataFormat) {
return $http.get(root + '?assetId=' + assetId + '&dataFormat=' + dataFormat;
};
// other routes
});
Note that $http can and should be used instead of $.ajax when you're using Angular. It does pretty much the same thing as $.ajax, except it plays nice with Angular.
This is a javascript function to make an upload of a photo to facebook, via facebook api.
It works everywhere, except in Internet Explorer 9
var sTagsPostcard =
'[{tag_uid:' + sIdProfile + ',x:' + aPositonsTags[sIdLayoutPostcard][0] + ',y:' + aPositonsTags[sIdLayoutPostcard][1] + '},' +
'{tag_uid:' + sIdFriend + ',x:' + aPositonsTags[sIdLayoutPostcard][2] + ',y:' + aPositonsTags[sIdLayoutPostcard][3] + '}]';
var the_url = $('input[name="hdnDomain"]').val() + 'media/postcards/' + sPathPostcard
$.ajax({
type: "POST",
url: "https://graph.facebook.com/" + sIdProfile + "/photos",
data: {
message: 'MESSAGE',
url: the_url,
format: "json",
access_token: sToken,
tags: sTagsPostcard
},
success: function(data){
if($('html').hasClass('touch'))
$('form[name="frmCustomLayout"]').submit();
else
window.parent.location.href = 'https://apps.facebook.com/[APPLICATION_NAME]/?st=' + N_STEP_THANKS;
},
error:function(a,b,c){
if($('html').hasClass('touch')){
$('input[name="st"]').val(N_STEP_RESULT);
$('form[name="frmCustomLayout"]').submit();
}
else
window.parent.location.href = 'https://apps.facebook.com/[APPLICATION_NAME]/?st=' + N_STEP_RESULT;
}
});
The execution enters the error() and not the success() function, and those are the parameters a,b,c:
a: object error (or sth like that)
b: error
c: no transport
Any ideas for IE9?? thanks in advance
The javascript ajax calling the graph.facebook api it seems not to work in browsers like IE9 / IE8.
The trick to solve it was to make an ajax call to a local script, and through that use the facebook api to do the same post.
In this way in all browsers the posting is working,
Here is my codes,
function getMods(name){
var init;
$.ajax({
type: "POST",
url: "init.php",
data: { 'id': getID(), 'Name': name },
cache: false,
success: function(data)
{
return data;
},
async:false
});
}
function init(){
var gm = '<br/>';
var gm1 = getMods('Name');
$('#brand').append(gm1);
var gm2 = getMods('Owner');
var gm3 = getMods('Admin1');
var gm4 = getMods('Admin2');
var gm5 = getMods('Admin3');
var gm6 = getMods('Admin4');
$('#online3').append(gm2 + gm + gm3 + gm + gm4 + gm + gm5 + gm + gm6 + gm);
}
The getMods() returned "undefined". When i use this code:
alert(data);
The page alerted the correct values.
Here is the php file init.php:
<?php
include('dbconnect.php');
if(isset($_POST['id']) && isset($_POST['Name'])){
$id = $_POST['id'];
$name = $_POST['Name'];
$init = chatMods($name,$id,$username,$password);
echo $init;
}
?>
And I add the async in the getMods().
You should make the distinction between a return value of a function (getMods() in your case) and a callback function (success).
In your code, you call getMods() which in fact doesn't return any value, hence you get undefined.
The success callback happens only after a while, after your function had already returned.
One possible solution for you (though not the greatest because it locks your JS processing) is to use an Ajax call in a synchronous way. A call that only returns when an answer from the server has been received. Use async: false in your $.ajax call, capture your return value and then return it. See this example: https://stackoverflow.com/a/2592780/290343
You should be aware, of course, that your Ajax calls might fail and you need to take that into account. Also, your code might run really slow because it does 6 remote calls to a server.