$.ajax not working in IE9 - javascript

I have the following code that is working on Chrome and Firefox, but not in IE9
$.ajax({
type : type,
url : url,
data : reqData,
crossDomain: true,
cache: false,
dataType : dataType,
contentType : contentType,
success : successFunction
}).fail(function() {
showError("IE9!");
});
type is POST, dataType is JSON and contentType is application/json
All the other parameters are correct
I've tried removing the contentType, removing the cache, setting cache to true, nothing seems to work
Any thoughts?
Thanks in advance

you cannot use normal ajax for cross domain access in ie, you have to use XDR for this purpose
please refer this link

Check the url path ( should be absolute ) and make it unique adding a timestamp for example
var url = 'http://mydomain.com/' ** not '/' **
var timestamp = new Date()
var uniqueUrl = url + (uri.indexOf("?") > 0 ? "&" : "?") + "timestamp=" + timestamp.getTime()
then
url : uniqueUrl,

Related

Special character in json response is not parsed in IE

I am using the following code in the js file to get the response back from servlet as follows:
The code is as follows:
function(response){
$("#city").attr("disabled", false);
var select = $('#cityList');
select.find('option').remove();
var parsedObject = JSON.parse(response);
for(var city in parsedObject){
$('<option>').val(parsedObject[city].cityName).text(parsedObject[city].cityName).appendTo(select);}}
In Internet Explorer, I get an error stating "Invalid Character" in the line:
var parsedObject = JSON.parse(response);
After checking the response, I noticed an object where the city name is causing an issue:
{"countryId":0,"stateId":0,"cityId":4046,"cityName":"Belle Vall�}
The correct city name is Belle Vallée. It seems IE is not allowing to parse this city name and hence, the drop down displays no city value.
In the Google Chrome, I do see all city values however, the above stated city name is not displayed properly as shown below:
I came across a solution to add the following code in the header to allow chrome to display special characters properly but it didn't work for me.
< meta http-equiv="Content-Type" content="text/html; charset=utf-8" >
The following code is being executed on the servlet side:
This is how my new function with ajax call looks like:
function changeState(){
var state = $("input#province").val();
$.ajax({
type: "GET",
url : 'LocationServlet',
dataType : "json",
contentType: "application/json; charset=utf-8",
data : {"stateName": state},
success : function(response){
$("#city").attr("disabled", false);
var select = $('#cityList');
select.find('option').remove();
var parsedObject = JSON.parse(response);
for(var city in parsedObject){
$('<option>').val(parsedObject[city].cityName).text(parsedObject[city].cityName).appendTo(select);
}
}
});
}
It seems that the response you have received has already been parsed.
Instead of the line:
var parsedObject = JSON.parse(response);
Try using "response" received as a parsed object.
Add
response.setCharacterEncoding("UTF-8");
If your response come from an ajax call. Just check the content-type of your ajax call.
$.ajax({
type: "GET",
dataType : "json",
contentType: "application/json; charset=utf-8",
...
})

AJAX POST of tinyMCE contents - Character encoding of table entities

I'm using the tinyMCE editor plugin in my webpage.
When I use a simple HTML page post to post the contents to the backend PHP and save them to a database or file, all seems OK.
When I try to do the same using an AJAX post, I am finding it impossible to prevent encoding issues.
In particular (I'll escape it here) \&\n\b\s\p\; is being converted to  and a "-" character is being converted to �
I've tried a few suggestions but with little luck.
The suggestion is that it is either an issue with my charset or encoding.
My save function is as follows:
function letterSave(newfile,fname){
var newDate = new Date;
var uniq=newDate.getTime();
var input = $('#myTextareastdletter');
var contents=input.val();
var context='<?=(!strcmp($sessobj->mode,"SA_ViewAbs")?"A":"S");?>';
// alert(fname);
$.ajax({
type: "POST",
url: '<?=auto_version("./SVajaxSaveDocContents.php")?>?duff='+uniq,
contentType: "application/x-www-form-urlencoded; charset=UTF-8",
data: {'S_sessid' : '<?=$sessobj->sessionid?>', 'context' : context, 'contents' : encodeURIComponent(contents), 'fname' : fname, 'newfile' : newfile},
success: function(data){
data = JSON.parse(data);
fname=data[0];
newLetterList=data[1];
if (fname != 'FAIL') {
alert ('OK: Letter Contents Saved to File ('+fname+').');
var versionOutput = $('#popupOutput');
versionOutput.html('');
var box = $('#popupDisplay');
var cover=$('#coverDiv');
box.css({
display:"none"
});
cover.css({
display:"none"
});
var letterOutput = $('#AbsenceLetters');
letterOutput.html(newLetterList);
} else {
alert ('Sorry: Failed to Save Letter Contents!');
}
},
error: function(data){
alert ('Sorry: Failed to Save Letter Contents!');
// console.log(data);
// console.log('error');
}
});
}
As you can see, I've been playing with setting the contentType and using encodeURIComponent().
Any help would be appreciated.
Cheers
In order to avoid these encoding issues, don't use .val on the textarea that the tinyMCE instance is attached to. Instead, you can utilize tinyMCE's built-in getContent method like so:
tinyMCEInstance.getContent()

Getting the host URL + the destination URL when

I am using an Ajax POST call to get some data from a file located on an other server, but I am getting my host URL + the destination URL as URL for my AJAX URL!
My host URL: 192.168.1.2
My destination URL: 192.168.1.7/var/www/html/pfe/ajax.php
How could I get the destination URL only?
var url = "192.168.1.7/var/www/html/pfe/ajax.php";
$("#c1.on").click(function () {
$.ajax({
url: url,
type: 'POST',
data: { on : true, pin : 16 },
success: function(data){
$("#c1.on").addClass("hide");
$("#c1.off").removeClass("hide");
}
});
});
The URL I get: 192.168.1.2/192.168.1.7/var/www/html/pfe/ajax.php
The URL syntax is incorrect. If a URL doesn't have // in it, it's treated as a filename on the same server as the current page. So it should be:
var url = "//192.168.1.7/var/www/html/pfe/ajax.php";
The // indicates that the next component is the name or address of the server.
Not that this still may not work because of the restriction against cross-domain AJAX.

How to simply read html from any url via javascript and proxy?

This is my proxy.php file:
$url = urldecode($_GET['url']);
$url = 'http://' . str_replace('http://', '', $url); // Avoid accessing the file system
echo file_get_contents($url); // You should probably use cURL. The concept is the same though
and my reader.js:
$(document).ready(function () {
var url = 'http://localhost/taboo.blue-world.pl/admin/proxy.php?url=' + encodeURIComponent('http://www.blue-world.pl')
$.ajax({
url : url,
type : 'GET',
dataType : 'json'
}).done(function(data) {
console.log(data.results.result[1].category); // Do whatever you want here
});
});
But it doesnt print anything. Can you help me solve it? I am not quite good with this.
Currently your are trying to get JSON response. Change dataType to html.
dataType: 'html'
It looks like you are trying to get an HTML response as JSON.
If the content is HTML you should turn you ajax call to:
$.ajax({
url : url,
type : 'GET',
dataType : 'html'
}).done(function(data) {
console.log(data); // data contains the html as a text
});
Use either dataType: 'html' in reader.js (to get HTML data)
OR
echo(json_encode(file_get_contents($url))); in proxy.php (for JSON data)
Try jQuery.parseJSON for json format
$.ajax({
url : url,
type : 'GET',
dataType : 'json'
}).done(function(data) {
var data = jQuery.parseJSON(data);
console.log(data.results.result[1].category); // Do whatever you want here
});
});

Angular $http.jsonp() method works only once

Response is succeded (alert->done), but second and another hits will response 'error'.
I've tried to add some config params with 'cache: false' but still works only first time. Should I clear some cache/history or sth?
$scope.add2 = function() {
var config = {
//url : 'http://www.***.pl/index.php/json/getallusers',
cache: false,
//type : 'POST',
crossdomain: true,
//callback: 'JSON_CALLBACK',
//data: d,
contentType: "application/json",
dataType: "jsonp",
};
var r = Math.floor(Math.random() * 1000) + 4;
var d = {user_type_id:0, user_venue_id:0, fname:r};
var e = objToString(d);
//$http.jsonp('http://www.***.pl/index.php/json/adduserget?callback=JSON_CALLBACK&'+ e, config)
$http.jsonp('http://www.***.pl/index.php/json/adduserget?callback=JSON_CALLBACK&'+ e)
.success(function(res){
console.log('res:' + res.user);
alert('done');
})
.error(function(){
console.log('error');
alert('error');
});
};
This is the new question in jsonp and post action in ionic framework (angular.js)
I've added to server response 'angular.callbacks._0(' before json data... maybe here's mistake?
This is solution for my issue:
now i'm dynamiccaly getting the callback parameter which can by vary (not always angular.callback_0, but can be: angular.callback_1, angular.callback_2, etc.) from GET method at a server and put it before response data f.e in php:
<?php header('content-type: application/json;');
$json=json_encode($result);
echo $_GET['callback'].'('.$json.')';
?>
Issue is because url is getting cached in browser and the other time it will fetch from the cache.So I'd suggest you to add new dummy parameter inside you URL that will have current Date.now() so that every time you call service that will make URL unique doe to Date.now() component.
Code
$http.jsonp('http://www.where2play.pl/index.php/json/adduserget?callback=JSON_CALLBACK&'+
e + '&dummy='+ Date.now()) //<--this will add new value everytime to make url unique

Categories

Resources