i have function that works in IE and does not work in other browsers
please help rewrite it for cross browser compatibility
function Fetch(xml) {
var Xml = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">";
Xml += GenerateAuthenticationHeader();
Xml += "<soap:Body>";
Xml += "<Fetch xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">";
Xml += "<fetchXml>";
Xml += CrmEncodeDecode.CrmXmlEncode(xml); // Microsoft _HtmlEncode function
Xml += "</fetchXml>";
Xml += "</Fetch>";
Xml += "</soap:Body>";
Xml += "</soap:Envelope>";
// Microsot CreateXmlHttp function
if ( XMLHttpRequest != null){
var XmlHttp = new XMLHttpRequest();
}
else{
var XmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
XmlHttp.open("POST", "/mscrmservices/2007/crmservice.asmx", false); //Sync Request
XmlHttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
XmlHttp.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Fetch");
XmlHttp.send(Xml);
var ie = (window.ActiveXObject) ? true : false;
var XmlDoc = (ie) ? new ActiveXObject("MSXML2.DOMDocument") : new window.XMLHttpRequest();
XmlDoc.async = false;
XmlDoc.resolveExternals = false;
XmlDoc.loadXML(XmlHttp.responseXML.text);
return XmlDoc;
}
how to change the code so that it works in google chrome
Try this code. I'm not sure if it'll work in Chrome, and make sure to see the comments about a possible issue with responseXML vs ResponseText, but it should make the response easier to handle.
And if you're in 2011, why the 2007 endpoint? It has been deprecated and is not even available for some instances of CRM online, and will not be available for any CRM 2013 instance.
I suggest incorporating the XrmServicesToolkit into your solution which is cross-browser and will greatly simplify executing a SOAP request:
var request = "<request i:type='b:WhoAmIRequest' xmlns:a='http://schemas.microsoft.com/xrm/2011/Contracts' xmlns:b='http://schemas.microsoft.com/crm/2011/Contracts'><a:Parameters xmlns:c='http://schemas.datacontract.org/2004/07/System.Collections.Generic' /><a:RequestId i:nil='true' /><a:RequestName>WhoAmI</a:RequestName></request>";
var whoAmI = XrmServiceToolkit.Soap.Execute(request);
Related
I have a Sharepoint 2013 site set up. I added a web part to the home page that makes an ajax call to an outside API, and returns JSON data, showing an image and a link. When you visit the site in Chrome, it works perfectly, however in Internet Explorer (11), it doesn't work. The odd part is I know that it is running the JS, because it is showing the "No Current News" p tag from the second line. This is my code:
<h1 style="text-align: center">PQA In The News</h1>
<div id="myNewsFeed"><p>No Current News</p></div>
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script><script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script><script type="text/javascript">
$(document).ready(function() {
document.getElementById("myNewsFeed").innerHTML = "<p>No Current News</p>";
var d = new Date();
var n = d.getTime();
var mo = 86400000 * 30;
n = n - mo;
var queryString = "https://webhose.io/search?token=xxxx&format=json&q=Company%20Name&ts=" + n;
loadJSON(queryString);
function loadJSON(newsURI){
var data_file = newsURI;
var http_request = new XMLHttpRequest();
try{
// Opera 8.0+, Firefox, Chrome, Safari
http_request = new XMLHttpRequest();
}catch (e){
// Internet Explorer Browsers
try{
http_request = new ActiveXObject("Msxml2.XMLHTTP");
}catch (e) {
try{
http_request = new ActiveXObject("Microsoft.XMLHTTP");
}catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
http_request.onreadystatechange = function(){
if (http_request.readyState == 4 ){
var jsonObj = JSON.parse(http_request.responseText);
DisplayData(jsonObj);
}
}
http_request.open("GET", data_file, true);
http_request.send();
}
function DisplayData(data) {
var htmlSource = "<ul>";
for(var i=0; i < data.posts.length; i++) {
htmlSource += "<li><img src=\"" + data.posts[i].thread.main_image + "\" height=\"150px\" width=\"150px\">" + data.posts[i].title + "</li>";
}
htmlSource += "</ul>"
document.getElementById("myNewsFeed").innerHTML = htmlSource;
}
});</script>
I changed the Query String, because it has my company's key, but I can assure you it works. On Chrome, this shows everything fine, but in IE it just shows "No Current News".
Did I do anything wrong, or does Internet Explorer have something against Ajax?
As suggested by Kalamarico best way is to use jquery ajax call and also add fail handler to check exact error. In the above code your javascript call is working but it might not be going to ready state 4 and hence not setting the value. Can you check for other states and also should not be the http request call inside readystatechange event inside else otherwise after hitting ready state 4 also it will fire again.
I'm trying to let a users on my site to save and XML file one the local machine and then later to load them using the HTML file element.
Saving the file what done with iFrame.
When trying to let the user load the file i am getting exceptions all the time.
I've tried every thing i could find over the web and can't seem to find the way to do it.
I am getting all kind of exception, like cross domain or XMLHttpRequest cannot load file:///C:/fakepath/Regions.xml. Cross origin requests are only supported for HTTP.
Depending on the code i tried.
I read that HTML5 standard replace the url with "fakepath", and can't find solution for this. Is there no way to let the user load a file from his own computer to be edited? loading a specific file from server is not a problem but i want to give this freedom to the user and not decide for them what file to load, and also to let them save and load the xml on their computer and not the server
Is there a solution for this problem?
Found this codes but neither helped (and I've tried few other veriations of this):
1)
var error = "";
strFile = document.frmLoadFile.selectedFile.value;
intPos = strFile.lastIndexOf("\\");
strDirectory = strFile.substring(0, intPos);
//alert(strDirectory);
document.frmLoadFile.selectedFile.value = strDirectory;
var file = 'file:\\\\\\' + document.frmLoadFile.selectedFile.value;
try //Internet Explorer
{
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
xmlDoc.load(file);
}
catch (e) {
try //Firefox, Mozilla, Opera, etc.
{
xmlDoc = document.implementation.createDocument("", "", null);
xmlDoc.async = false;
xmlDoc.load(file);
}
catch (e) {
try //Google Chrome
{
var xmlhttp = new window.XMLHttpRequest();
xmlhttp.open("GET", file, false);
xmlhttp.send(null);
xmlDoc = xmlhttp.responseXML.documentElement;
}
catch (e) {
error = e.message;
}
}
}
2)
var xmlDoc;
var xmlloaded = false;
function xml_initLibrary(file) {
importXML(file);
}
function importXML(xmlfile) {
try {
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", xmlfile, false);
}
catch (Exception) {
var ie = (typeof window.ActiveXObject != 'undefined');
if (ie) {
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
while (xmlDoc.readyState != 4) { };
xmlDoc.load(xmlfile);
xmlloaded = true;
readXML();
}
else {
xmlDoc = document.implementation.createDocument("", "", null);
xmlDoc.onload = readXML;
xmlDoc.load(xmlfile);
xmlloaded = true;
}
}
if (!xmlloaded) {
xmlhttp.setRequestHeader('Content-Type', 'text/xml')
xmlhttp.send("");
xmlDoc = xmlhttp.responseXML;
xmlloaded = true;
readXML();
}
}
function readXML() {
//console.log(xmlDoc);
}
does any one knows if there is a way to fix this? of do you need to save the files on the server?
Thank you all very much
Erez
I think you're looking for FileReader, new in HTML5. For IE < 10 you'll need to use the ActiveX FileSystemObject.
This code works for me on Chrome.
<script type="text/javascript">
function doit(e) {
var files = e.target.files;
var reader = new FileReader();
reader.onload = function() {
var parsed = new DOMParser().parseFromString(this.result, "text/xml");
console.log(parsed);
};
reader.readAsText(files[0]);
}
document.getElementById("selectfile").addEventListener("change", doit, false);
</script>
<input type="file" id="selectfile" />
http://jsfiddle.net/xKuPV/
Hi so I have a XML document in a folder on my desktop, I am trying to read out all the elements with the tag name "cuisine".
Here is my code:
if(window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
}else{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","data.xml", false);
xmlhttp.send()
xmlData = xmlhttp.responseXML;
cuisineList = xmlData.getElementsByTagName("cuisine");
document.getElementById("test").innerHTML = cuisineList.length;
When I print out the length of the cuisineList it says its zero.
Here is my XML document:
<?xml version="1.0" encoding"ISO-8859-1"?>
<food>
<cuisine type="Chinese">
</cuisine>
<cuisine type="Indian">
</cuisine>
</food>
I think you have an error in your document (there is a missing '=' in the encoding attribute).
I wonder whether someone can help me please.
I've put together this page, which will give the user functionality to load markers with the selection or deselection of checkboxes. They will then be able to click on these markers which in turn shows the nearest POI within a given radius.
The problem I'm having is with loading the xml at lines 143-145 which are:
downloadUrl("loadmylocations.php", function(data) {
var xml = data.responseXML;
var markers = xml.documentElement.getElementsByTagName("marker")
Firefox tells me that the error is that the 'xml is undefined'. I think I know why and that's because I'm using this code to pull data for the POI's.
var radius = document.getElementById('radiusSelect').value;
var searchUrl = 'searchsites.php?siteosgb36lat=' + center.lat() + '&siteosgb36lon=' + center.lng() + '&radius=' + radius;
downloadUrl(searchUrl, function(data) {
var xml = parseXml(data);
So they clash, because I define the XML at the end of the script differently, these being
loadmylocations.php
request.open('GET', url, true);
request.send(null);
}
function doNothing() {}
searchsites.php
request.open('GET', url, true);
request.send(null);
}
function parseXml(str) {
if (window.ActiveXObject) {
var doc = new ActiveXObject('Microsoft.XMLDOM');
doc.loadXML(str);
return doc;
} else if (window.DOMParser) {
return (new DOMParser).parseFromString(str, 'text/xml');
}
}
function doNothing() {}
Could someone perhaps tell me please how I can change so that I can pull the two sets of data wihtin the same script without them clashing.
Many thanks and kind regards
I did some work around this, and through trial and error I discovered that I could load the information for the 'loadmylocations' script as the 'searchsites' script i.e. var xml = parseXml(data); rather than var xml = data.responseXML;.
I just started using JSON and found this example from http://imdbapi.com/:
<script type="text/javascript">
// IMDb ID to Search
var imdbLink = "tt1285016";
// Send Request
var http = new ActiveXObject("Microsoft.XMLHTTP");
http.open("GET", "http://www.imdbapi.com/?i=" + imdbLink, false);
http.send(null);
// Response to JSON
var imdbData = http.responseText;
var imdbJSON = eval("(" + imdbData + ")");
// Returns Movie Title
alert(imdbJSON.Title);
</script>
But it just returns a blank page. What is wrong?
I'm sorry not to directly answer your question, but here is a jQuery version:
var imdbLink = "tt1285016";
// Send Request
$.getJSON("http://www.imdbapi.com/?i=" + imdbLink + "&callback=?", function(data) {
alert(JSON.stringify(data));
});
There are a couple possible issues with your code.
1.) ActiveX is IE only, not firefox, chrome, safari, etc.
2.) You have a cross-domain issue.
Example Fiddle