Apache Cordova and Jquery-Ajax Requests - php

I just try with Apache Cordova and normal jQuery to make an Ajax-Request.
I have this code so far:
function onDeviceReady() {
// Handle the Cordova pause and resume events
document.addEventListener( 'pause', onPause.bind( this ), false );
document.addEventListener( 'resume', onResume.bind( this ), false );
// TODO: Cordova has been loaded. Perform any initialization that requires Cordova here.
$(function () {
var datastring = "test";
$.ajax({
type: "POST",
url: "serverfiles/app-db-connection.php",
data: dataString,
success: function (data) {
$('#result').html(data);
alert("test");
}
});
$('#result').html("test");
});
};
the .php file is local and works, if I call it through the browser. Also the ajax-Part works fine in other normal webprojects.
So, what should I care about, then useing cordova. There have to be some issues with cordova.
Even the test-alert isn't working.
A little hints how normal Ajax-requests work with cordova would help me a lot.
Normal Jquery-Code is working fine on the test-machines.

Ok. just solve the problem.
2 mistakes done:
first: Var datastring and "dataString" were not written equal.
second: it is not allowed to use in url-property a relative file-path. This because the file will not compiled by the app it self... it has to be an absolute path: http://blabla.com ... not ir works fine.

Related

$.ajax not a function in yii

I have the below function that I used in two different pages and worked perfectly in both cases. Suddenly my pages stopped updating and I got the error $.ajax not a function.
function update_report(data) {
var request = $.ajax({
url: "report?poids="+data,
type: "GET",
dataType: "html"
});
request.done(function(msg) {
$("#yw2").html(msg);
});
request.fail(function(jqXHR, textStatus) {
alert( "Request failed: " + textStatus );
});
req ="a";
}
I then changed the $ to jQuery. Then it started sending AJAX requests but it didnt get the relative URL. Like I was generating the request from a page like /index.php/link1 and my code was sending request to /index.php/report rather then /index.php/link1/report
So, then I changed my url: to "link1/report=?"+data then it started sending request to the right page but couldnt update the response as it didnt find the div #id. Got error
TypeError: $(...) is null
What can cause all this issue and how to fix this? And why $.ajax suddenly stopped working, though I didnt make any changes?
JQuery can be not available by $.ajax(). Check if jQuery.ajax() is available or you included jquery twice.
You should not require jquery manually. The best way to include jquery is using Yii::app()->clientScript->registerCoreScript('jquery'). Path for library is set in config/main.php.

why is jqXHR.responseText returning my PHP file and not executing the script?

I'm trying to simply execute an ajax request to my server. The request passes my form data to signUp.php where the information is then process. Then php will echo back a responseText to my jqXHR object and I print the alert. The problem is that my php file is being executed, rather the jqXHR.responseText is instead returning the my php file itself as if it were a text file. A sample php responseTest would look like ...
"<?php
php code ...
?>"
Instead I want the responseText to return my echoes. The code is written bellow.
var formData = new FormData(document.getElementById("signUpForm"));
$.ajax({
url: "./cgi-script/signUp.php",
type: "POST",
xhr: function giveXmlHTTP(){
myXhr = $.ajaxSettings.xhr();
if(myXhr.upload){
myXhr.upload.addEventListener('progress',progressHandler, false);
}
return myXhr;
},
success: function(data,statusText,jqXHR){
alert(jqXHR.responseText);
},
data:formData,
cache: false,
contentType: false,
processData: false
});
}
These generally happens when PHP does not gets parsed. So make sure you are running on the SERVER which is capable and configured to run PHP and are not double clicking the HTML page.
The two most common causes for this problem are that you are missing the initial <?php and that your server has not been set up for PHP. My guess is that when you just visit signUp.php you'll find the same problem.

AJAX function call within Joomla Component

I have a good understanding of AJAX and normally would have no problems using it but I am relatively new to Joomla and have only recently started building components, etc.
I have created a component (named directory) which is using the 'default' view. Within here I have the following code which is an AJAX call:
<script type="text/javascript">
var url = "index.php?option=com_directory&view=directory&task=clubFilter&format=raw";
jQuery(document).ready(function() {
jQuery('#city').change(function() {
jQuery.ajax({
url: url,
type: "POST",
data: jQuery("#city").serialize(),
dataType: 'json',
success: function(data) {
alert('data');
}
});
});
});
And within my 'views/directory/views.html' file I have created the following function which currently contains a die so I can confirm when it is working:
public function clubFilter() {
die(print_r('here_i_am'));
}
When I run the following code I ge the following error within Firebugs console..
'Error: 500 View not found [name, type, prefix]: directory, raw, directoryView'
I think it is because of the AJAX url var being incorrect but I have tried many different solutions from here nad other sources and just can't get the AJAX function working. Is my URL wrong?
Many Thanks
Normally, I make ajax calls to a task on the controller.
Here is the url format I am using in one of my extensions that uses ajax calls to a component:
index.php?format=raw&option=<component_name_goes_here>&task=<task_goes_here>
Then, in my component's default controller I put a function with the same name as the task:
function getSomeData()
{
echo(json_encode($data));//I normally return json
}
Hope this helps.
try this url. it might help you out.
var url = "index.php?option=com_directory&view=directory&task=clubFilter&tmpl=component";
format=raw replace with &tmpl=component
Else you can try the below format too.
jQuery.post('index.php',{
'option':'component_name',
'controller':'controller_name',
'task':'task_name',
'format':'raw',
'data': jQuery("#city").serialize(),
'dataType': 'json',
},function(result){
//edit the result here
return;
});
If you have any problem regarding this don't hesitate to ask.

Passing php file in ajax

This is my code
<script>
$(document).ready(function() {
// delete the entry once we have confirmed that it should be deleted
$('.delete').click(function() {
var parent = $(this).closest('tr');
$.ajax({
type: 'POST',
url: 'delete.php',
data: 'ajax=1&delete=' + $(this).attr('id'),
success: function() {
parent.fadeOut(300,function() {
parent.remove();
});
}
});
});
$('.delete').confirm({
});
});
</script>
My question is why delete.php is not executing? It's in the same folder, do I need type absolure url to this file? But the best of it is that row in table is deleting, but php file is not executing.
And second question is why this code is not working without this line:
$('.delete').confirm({
});
In php file i got this to know is it executed:
<script>
alert('aaaa');
</script>
Or even I had change it on echo 'something'; but still not working
If the row is deleted the certainly the php file is executed and you have to check the url data as #Saifuddin has described.
use firebug console to see your result. and also you are not passing parameter to the success function
so change this
data: { ajax: 1, delete: $(this).attr('id')},
and also this
success: function(msg) {
alert(msg)
parent.fadeOut(300,function() {
parent.remove();
});
}
I assume that the php file is executed in your case, while better approach to define the url with absolute path like http://www.example.com/delete.php. however the way you pass data is not as per standard.
so replace this line.
data: 'ajax=1&delete=' + $(this).attr('id'),
with
data: { ajax: 1, delete: $(this).attr('id')},
and for debugging print $_POST variable in your php file.
hope it helps.
I would add an error handler to see what is happening with the ajax call, something like this:
error: function(jqXHR, textStatus, errorThrown) {
console.log(errorThrown);
}
Keep in mind that the ajax call gets the current directory preppended to the relative uri you set. I assume you are sending the request to a working web server, i.e. using the HTTP protocol.
There is no confirm() jQuery function as far as I know. You usually do:
if (confirm('Are you sure you want to delete this item?')) do_delete();
The confirm() method returns true when the user selects OK, and false otherwise.
Cheers.

How can I implement a jquery ajax form which requests information from a web api via a php request?

I'm trying to implement a simple api request to the SEOmoz linkscape api. It works if I only use the php file but I want to do an ajax request using jquery to make it faster and more user friendly. Here is the javascript code I'm trying to use:
$(document).ready(function(){
$('#submit').click(function() {
var url=$('#url').val();
$.ajax({
type: "POST",
url: "api_sample.php",
data: url,
cache: false,
success: function(html){
$("#results").append(html);
}
});
});
});
And here is the part of the php file where it takes the value:
$objectURL = $_POST['url'];
I've been working on it all day and can't seem to find the answer... I know the php code works as it returns a valid json object and displays correctly when I do it that way. I just can't get the ajax to show anything at all!!
...data: 'url='+encodeURIComponent(url),

Categories