I want to load an HTML div from an html file hosted on windows azure blob (own by me) into a MVC view which is also hosted on windows azure web role ( Both the blob and the web role are owned by me ).
I tried using jquery.load() but it runs into cross domain issues and window azure blobs do not allow changing the CORS policy. Next i am trying to use Require.Js and Text.Js to achieve this cross domain load.
From the Text.Js documentation :
Text plugin determines that the request for the resource is on another domain, it will try to access a ".js" version of the resource by using a script tag. Script tag GET requests are allowed across domains. The .js version of the resource should just be a script with a define() call in it that returns a string for the module value.
My js code for cross domain call with require.js is :
require(["text!http://xxxxx.blob.core.windows.net/xxx/File"],
function (html) {
alert(html);
}
);
Now the issue which i am facing is that the File's .js version was successfully loaded by the plugin (Confirmed by using chrome inspector) but in the callback when i tried to access the text content of the file , it show me undefined. What i am doing wrong here, why the callback does not have the text content? Can anybody help me to solve this issue ? In case this is not solvable, i am open to other ways to achieve this cross domain load.
My understanding from require.js documentation is that cross domain calls can be accomplished by call a javascript version of the resource.
Try with define:
define(["text!http://xxxxx.blob.core.windows.net/xxx/File"], function (html) {
return {
template: html;
}:
}
);
Related
I recently made my website SSL certified, but now my calculator is broken.
Url: https://secondunitcentersmc.org/calculator/
Please help provide a solution, the only affected drop down is the first location one.enter image description here
Console Log Error:
angular.js:12011 Mixed Content: The page at 'https://secondunitcentersmc.org/calculator/' was loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint 'http://secondunitcentersmc.org/wp-content/themes/secondunitcenter/calc/data/rents.json'. This request has been blocked; the content must be served over HTTPS.
Changing this URL to https does not work, (it causes the form to not appear) neither does changing $http to $https in both locations within the file
Image from scripts/app.js of my website. It is also a wordpress site.
So When I navigate to http://secondunitcentersmc.org/wp-content/themes/secondunitcenter/calc/data/rents.json it is automatically changing to https. In your js file where you make the call to this page, does the url there start with http or https?
Instead of using a json file for this data, you might want to consider creating a private custom post type for the locations, then you can just use the standard wordpress query to call them into the drop down. It will also be easier to add and edit them in the future. (and get rid of your https/http error :))
How did you add https to your site? with a plugin or manually? If manually- you need to update your site address and wordpress address in general>settings as well as add some code to your .htaccess file. more info here (method #2) https://www.wpbeginner.com/wp-tutorials/how-to-add-ssl-and-https-in-wordpress/
I am writing a Chrome Extensions for reddit.com and would like to read the contents of a CSS file that website has already loaded.
In my research I've come across two methods:
document.styleSheets
When I console.log( document.styleSheets ) however, the "cssRules" and "rules" are both "null" for some reason, however if I load the css file url in the browser, there are plenty of rules:
https://b.thumbs.redditmedia.com/qFTJmHhu1fP0DVZHvATz60WXl-MhvSmCFoByN26OkoU.css
As you can see though, the css is hosted at redditmedia.com, so is this a cross-domain issue?
$.get
When I try to do an ajax call to read the contents of the file, it's giving me a "No 'Access-Control-Allow-Origin' header is present" message, which makes sense. It's hosted at a different domain.
Is there any way I can get the contents of this file?
Thanks for your help!
So I ended up using an ajax call to my server which does a file_get_contents() on the css file, and pass the contents back.
I load a local html file (from assets folder) to the app WebView.
In the HTML I run a jQuery.getJSON(url). the url is a remote server.
This action fails, and I'm guessing because of a different origin issue (cross domain). I run the same file on chrome and there it specifically says so.
Is there a way to allow the WebView in Android to load data from remote server on a local loaded HTML file?
Today morning I found solution that seems to be working.
The Java part
Initialize your WebView:
WebView _webView = (WebView) this.findViewById(R.id.id_of_your_webview_in_layout);
get WebView settings:
WebSettings settings = _webView.getSettings();
set following settings:
settings.setJavaScriptEnabled(true);
settings.setAllowFileAccessFromFileURLs(true); //Maybe you don't need this rule
settings.setAllowUniversalAccessFromFileURLs(true);
now you can load your your html file by standard way:
_webView.loadUrl("file:///android_asset/www/index.html");
The Javascript part
Create XHR request by standard way
var xhr = new XMLHttpRequest();
xhr.open("get", "http://google.com", false);
xhr.send();
Print the result somewhere
document.body.innerHTML = xhr.responseText
NOTICE:
This procedure works only on API level 16 or higher (At least the documentation says that).
Don't forget to add the internet permission in your manifest file:
<uses-permission android:name="android.permission.INTERNET"/>
Also make sure you are using JSONP requests (don't forget the &callback=? as stated above)
I load a local html file (from assets folder) to the app WebView
Note that you failed to say how you are doing this. I am going to guess that it was by a loadUrl() on a file:///android_asset URL.
Is there a way to allow the WebView in Android to load data from remote server on a local loaded HTML file?
Try using loadDataWithBaseURL() to load the content, supplying a URL on the remote server as the base URL.
Ajax calls wont work from local file system. More over it will become cross-domain. So it wont work. It worked in eclipse, becuz you tried from local server.
A solution we used was to use Android to get the update files from the server, place them and overwrite files in the web folder, and then browse.
I'm building a local html file to show dynamically some data from an XML file and by using Chrome's Inspector I figured my XML file is not being parsed because it is "not hosted on a webserver"
XMLHttpRequest cannot load data.xml. Cross origin requests are only supported for HTTP.
I know that there are a few flags I could pass to Chrome/web browser to workaround this limitation but I'm looking into some alternative method. I will probably have to distribute this files to a few people and teaching them how to pass flags to the browser is not an option for me. Hosting them on a web server is not an option either.
Thanks a lot in advance.
No ghost unless you set up a local server or host the XML file locally. Ajax has to follow the same origin policy.
If you're willing to use a library you can use jQuery's AJAX method to perform a cross-domain request (i'm not entirely certain that jQuery will support what you're trying to do). JSONP works, but you have XML data...
You could try loading it in a script tag anyway and see if you can get the innerHTML value without breaking the script; once you're done getting the text from it, remove the script from the page. You may be able to get at the data before the browser tries to parse the script by attaching onload and onreadystatechange events to the script element.
var s = document.createElement('script');
s.src = '/path/to/file.xml';
s.onload = s.onreadystatechange = getData;
function getData(e)
{
//get the text out of the script element
//remove the event handler from the script element
//remove the script from the page
}
document.getElementsByTagName('body')[0].appendChild(s);
I didn't test it, but it might work.
How about setting up a local webserver? XAMPP should be easy to install even for novice. Just tell them to put your files in the htdocs folder, run xampp and start the apache server.
Unless there's a compelling reason to have a separate html and xml file, you could just put the data directly in the html file.
I'm afraid if chrome only provides options that you don't like to apply, your application and chrome will not come together. Access via iframe & object does'nt work too, load()-method of createDocument is not supported by chrome(if it does I guess you got the same error).
Whatever you try will be a sideway to pass chrome's restrictions, what cannot be good, because I think they have good reasons to setup these restrictions.
Is it possible for me to supply a client with a snippet of HTML which contains a reference to a javascript file that I host? They want to paste this HTML into their CMS, so that when their page loads, it'll load our content.
I was under the impression that there was cross domain security preventing this from being possible.
What if, instead of linking to the JavaScript, I gave them the snippet of HTML with the JavaScript already included
so instead of
<div>
<!-- link to js -->
</div>
I gave them
<div>
$.get(/*url to my content*/);
</div>
Would that work?
You could use JSONP to simulate cross domain AJAX calls (works only with GET requests as internally it uses a script tag):
$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json&jsoncallback=?",
function(data) {
$.each(data.items, function(i,item) {
$("<img/>").attr("src", item.media.m).appendTo("#images");
if ( i == 3 ) return false;
});
}
);
Is it possible for me to supply a client with a snippet of HTML which contains a reference to a javascript file that I host?
Yes. The src of script elements has no same origin limits.
$.get(/*url to my content*/);
XMLHttpRequests still do have same origin limits. XHR can only fetch from the domain of the page, not the script.
The HTML <script> tags are exempt from the same origin policy, so if your client links to your JavaScript file with <script> tags, you will not have any problems. (Source)
Referencing a javascript file from a different domain is no problem. This is not cross site scripting, it's simply a cross site HTTP request. This is used a lot, e.g. by Google's JavaScript API Loader.