Why isn't this Jquery Ajax working in Opera and Safari? - php

This code works in FF, Chrome, IE6/8 but not in Safari and Opera.
Any ideas why?
Here is the code:
var name = $('#esm').val();
var email = $('#nam').val();
var message = $('#med').val();
var ad_id = $('#i_d').val();
var data_string = 'esm='+ name + '&nam=' + email + '&med=' + message + '&i_d=' + ad_id;
$.ajax({
type: "POST",
url: "/my_php_file.php",
data: data_string,
success: function(data) {
$('#tip_loader').hide();
if(data==1){alert('success'); }
else {alert('error'); }
}//end success function
}) //end ajax call
I have located the error to exactly the "Ajax" call, because when I put an alertbox just before the $.ajax the alert shows up correctly.
However, if I put the alertbox in the success function, nothing shows up, no alert.
This only happens in Opera and Safari...
EDIT:
FYI: I include this javascript file into a php file, and I also include the jquery.js file into the php file. So this is all in an external file.
EDIT:
/main.php
/bin/jquery.js
/bin/tip.js
/bin/tip.php
I include the above js files into main.php, and the form action in main.php is set to /bin/tip.php
And the path to the ajax url is /bin/tip.php instead of my_php_file.php

In Opera, by default Allow File XMLHttpRequest is false. So you need to change the settings. Open Opera browser, type about:config. It will take you to the Preference screen. Go to User Prefs folder, you can see a settings Allow File XMLHttpRequest. Check that and then Save. It should work.

Opera has a debugging tool built in called Dragonfly. Go to the Tools menu -> Advanced ->Opera Dragonfly
If you don't have the File menu bar, click Menu -> Page -> Developer Tools -> Open Opera Dragonfly
Once you have it open (open it on the page that you're working on), click the Scripts tab (it'll probably ask you to refresh the page, do that) and drop down to your external js file.
Once you've found your code, you can set a breakpoint on the $.ajax() line by clicking on the line number on the left side. Now, trigger your code and you'll see that it will break on that JavaScript line. You can then use the inspection tab (bottom, middle) to ensure that all of your variables are set correctly. You can also step through and debug the JavaScript.
The other thing you'll want to do is add an error function like so:
$.ajax({
type: "POST",
url: "/my_php_file.php",
data: data_string,
success: function(data) {
$('#tip_loader').hide();
if (data == 1) { alert('success'); }
else { alert('error'); }
}, //end success function
error: function(xhr, textStatus, errorThrown) {
alert(errorThrown);
}
}); //end ajax call
See if that gives you any more information.
Also, check the error console as #Mufasa suggested. It can be found under the Error Console tab in Dragonfly.

There isn't really a way to tell. You didn't post the php file. Without knowing the output, we can't determine how the browser will respond.
Other tips though, #1 you are not using encodeURIComponent for any of the values being passed it. Its much more simple to get jQuery to do it for you,
instead of data: data_string, you should have
data: { esm: name, nam: email, med: message, "i_d": ad_id }
jQuery will create the query string for you and correctly.

Related

Pass external URL into ajax javascript

I am trying to fetch the contents of an external XML URL, into the javascript's ajax script, but it simply won't accept it, due to CROSS domain issue. I have searched through google regarding this issue, but nothing good so far, everything I've read so far stated that I need to incorporate PHP as well, but with that, the page would take reload.
I simply want that when a user enters a number that number gets passed as an argument in the URL, then displays the XML content in the screen without reloading.
SO any help will be highly appreciated.
Here's my code so far.
<script>
function myFunction() {
$("#dvContent").append("<ul></ul>");
$.ajax({
type: "GET",
url: "http://lx2.loc.gov:210/lcdb?operation=searchRetrieve&recordSchema=marcxml&version=1.1&maximumRecords=1&query=bath.isbn%3D9781452110103",
dataType: "xml",
success: function(xml){
$(xml).find('record').each(function(){
var sTitle = $(this).find('datafield[tag="245"]').find('subfield[code="a"]').text();
var sAuthor = $(this).find('datafield[tag="100"]').find('subfield[code="a"]').text();
var sIsbn = $(this).find('datafield[tag="020"]').find('subfield[code="a"]').text();
$(".mypanel").html(text);
});
$("<li></li>").html(sTitle).appendTo("#dvContent ul");
$("<li></li>").html(sAuthor).appendTo("#dvContent ul");
$("<li></li>").html(sIsbn).appendTo("#dvContent ul");
});
},
error: function() {
alert("An error occurred while processing XML file.");
}
});
}
</script>

$.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.

jQuery ajax call to php script returning "[object Object]"

I have a jQuery ajax call with the following code:
var dataString = 'title=' + title;
alert ('datastring: ' + dataString); // This reports the correct value
$.ajax({
type: 'POST',
url: 'script.php',
data: dataString,
cache: false,
success: function(newTableID) {
alert ('newTableID: ' + newTableID);
// Do some stuff with the ID
},
error: function(response) {
alert('failed: ' + response);
// The above displays "failed: [object Object]"
}
});
It doesn't matter what I have in my php script, I still get the same result: The alert in the error part of the ajax call displays a message box showing "failed: [object Object]". I've even tried a simple echo in the php script - I don't think the php script is running at all from this ajax call.
The url for the script is correct - the js file and the script.php file are in the same folder.
Can anyone shed some light on what I may be missing here?
Thanks.
If you receive a failed, it's because your script.php is not available.
You can try to use console.log(response) instead of alert() and you should see what is into your variable with Firefox or Chrome debugging tools.
The error callback is being passed an object, but alert() can only show strings. Apart from some magic cases, turning an object into a string in JS gives you '[Object object]'.
Try using console.log instead of alert, which will show you the structure of the object in your browser's debug console (usually summoned with F12)
Also in the console (or if you install Firebug) will be a panel showing the network requests and responses, which may give you an idea of why there is an error.
You have to place : instead of =
var dataString = 'title:' + title;

xhr return value from a php page

Ok guys im a bit stuck here. I usually use jquery to do this but i found out it cant be done with jquery so im doing it this way ok so this is my code
var url = ("upload.php?loc="+uplocation);
var xhr = new XMLHttpRequest();
if(xhr.upload){ // check if upload property exists
xhr.open("POST", url, true);
xhr.setRequestHeader("X_FILENAME", file.name);
xhr.send(file);
}
And all it does is sends a file to a php page, but the php page doesn't upload the image which isn't what i want, so is their anyway of returning all the contents thats displayed on the page
if it was jquery i would do something like this
$.ajax({
type: "POST",
url: 'json/submitsongs.php',
data: loca,
success: function(data){
alert(data);
}
});
so my question is how to do return what ever is echoed on the php page and alert it(for debugging reasons).
thanks for your help
You would add an event listener for whatever event you desired (load, error, progress, etc). So in your case you would use the 'load' event which signifies that the file has finished loading:
xhr.addEventListener('load', onComplete, false);
The onComplete is your callback function which has the event as a parameter. This event contains the response text:
function onComplete(event) {
alert(event.target.responseText);
}
As of today, u can not upload async using Ajax.
Either use Iframes (like Google) or some nifty Flash (or Java) upload app.
Not sure, but might be HTML5 has a solution for that, but it won't be cross browser.

How can jQuery .load fail?

I am using a simple ajax loader to get content on wordpress.
$("#page_preview").load("ajaxloader/", function(response, status, xhr) {
if (status == "error") {
alert("Sorry but there was an error");
}
else
{
$('#page_preview').fadeIn(300);
}
});
return;
When I load a specific post that has a google map embedded, obviously something goes wrong BUT instead of going inside the if statement, the firebug shows that it goes beyond this code. Neither if or else hit.
Using the eclipse debugger I found that the page load successfully, but when it returns the data to the .load() method, the last breaks.
Any ideas on what might going on?
How
<script>
// as of jQuery 1.5.x ++
var pagePreview = $("#page_preview");
$.get("ajaxloader/").success(
function(response, status, jqXhr) {
alert("Success!");
pagePreview.empty();
pagePreview.append(response);
// i feel you need to parse the response for the google map div, and perform another $.get() of the google url?
}).error(function(response, status, jqXhr) {
alert("These are not the droids you are looking for. move along.");
}).complete(function(response, status, jqXhr) {
alert("Complete!");
});
</script>
#Why
jQuery.load() will get you on the documentation. .load is equivalent to this
It is roughly equivalent to $.get(url, data, success) except that it
is a method rather than global function and it has an implicit
callback function. When a successful response is detected (i.e. when
textStatus is "success" or "notmodified"), .load() sets the HTML
contents of the matched element to the returned data.
You need to register in $.ajax( complete:function(responseText, textStatus, XMLHttpRequest)){}); and check the textStatus value. if ok, then load the data into the destination $('selector') yourself. Or fix the .load() by watching your network xmlHttpRequests in chrome (ctrl + shift +j) or some network tab in firebug and debug the issue with your 'url' value in $('selector').load( 'url', function(response, status, xhr){})
A simple way to debug this is to look at the XHR requests with the firebug console tab. There might be a problem with the request URL and you might not get any data back, or maybe you get an error or something. With firebug you could easily see what the server returns.
Since .load only load on success, you could add a
console.debug("In .load function");
at the begging of the function. Again, checking with firebug you could find out if the .load callback is actually fired.
use $.ajax function
eg
$.ajax({
url: "test.html",
context: document.body,
success: function(){
//when Successfully executed
},
error: function(){
//When Error Fires
}
});

Categories