<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Booking Form</title>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.js" type="text/javascript"></script>
</head>
<body>
<script>
// Specify the url of the whitelabel.
var brandUrl = "https://website.com/sub-directory";
// Specify the products to include in the search. [car = 1, bike = 2, walk = 3]
var productTypes = "1,2,3";
// Display form header?
var showTitle = true;
var useCustomCss = false;
// Specify id of the search form parent wrapper - This is the element the forms will be inserted
var parentId = "RemoteSearchForm";
var remoteFormInit = function (data) { $('#' + parentId).html(data.result); }
$(function () {
var remoteUrl = brandUrl + "/remoteforms/GetForms?types=" + productTypes
+ "&showTitle=" + showTitle
+ "&customCss=" + useCustomCss;
$.ajax({ url: remoteUrl, dataType: 'jsonp' });
});
</script>
<div id="RemoteSearchForm"></div>
</body>
</html>
However, when I view the page, it appears blank. I’ve tried saving it as .html and .php, but the same thing happens.
When looking in the console, I see the following error:
Refused to execute script from [working url] because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.
Looks as though the script at remoteUrl is being served as text/html rather than text/javascript. You’ll need to alter the server-side script to set the correct Content-Type header.
If the server is running PHP, you do something like this:
Thanks, I tried the following as I read the text/javascript is now obsolete. However, both Content-Types display the HTML on the page rather than rendering the actual code.
Hmm… I’ve just reread the code in your original post. What is it that should be returned from the Ajax call? Because it looks like maybe you’re trying to return HTML and insert it into the current page?
Right, in that case ignore what I said about setting the Content-Type header - you can remove that from the script.
jQuery has a built-in method to do exactly what you want: load()
var remoteUrl = brandUrl + "/remoteforms/GetForms?types=" + productTypes
+ "&showTitle=" + showTitle
+ "&customCss=" + useCustomCss;
// fetch HTML via Ajax and insert into the container
$( "#RemoteSearchForm" ).load(remoteUrl);
“The character encoding of the HTML document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. The character encoding of the page must be declared in the document or in the transfer protocol.”
XMLHttpRequest cannot load [working url] No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://website.com' is therefore not allowed access.