Load page trought AJAX, get file from location.hash - javascript

When i try to do this:
$.ajax ({
url: location.hash,
method: 'GET',
dataType: 'html',
success: function (data, textStatus, jqXHR) {
$('#content').html(data);
},
error: function (jqXHR, textStatus, errorThrown) {
console.log(textStatus);
}
});
I get this error:
Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check http://xhr.spec.whatwg.org/.
My url is: localhost/index.php#test.php
It should use test.php as url to load.
What i need is when there is a hash in the url, i need to load the hash (filename)..
Edit:
When i add:
async: false,
I still get the same error..
Edit:
I am using jQuery version 2.1.3

Related

Pass Cookie though Ajax Call or use $_COOKIE in PHP

Is there a specific reason why passing a cookie through an AJAX call would or would not have an impact on performance or maintaining code as opposed to simply calling $_COOKIE inside of PHP?
In this case, I use this plugin to get cookies in javascript: Cookie Plugin
Example of passing cookie through Ajax call:
$.ajax({
url: 'getCaseManagerReport.php',
method: 'POST',
dataType: 'json',
async: true,
data: {activeDateSelected: Cookies.get('activeDateSelected').replace(/[+]/g, " "), activeProductSelected: Cookies.get('activeProductSelected')}
})
.done(function(response) {
//do stuff
})
.fail(function(jqXHR, textStatus, errorThrown) {
alert("There was a problem during verification. Please contact your System Administrator...");
console.log(jqXHR);
console.log(textStatus);
console.log(errorThrown);
});
});
Then in getCaseManagerReport.php
$activeDateSelected = $_POST[‘activeDateSelected’];
VS using $_COOKIE to retrieve cookie
$activeDateSelected = $_COOKIE[‘activeDateSelected’];

Jquery ajax call for generate authorization code

As part of my requirement, I would like to generate authorization code by calling O365 url from jquery ajax call. I am calling below script from document ready()
$.ajax({
type: 'GET',
dataType: 'jsonp',
async: false,
url: 'https://login.microsoftonline.com/{{tenant id}}/oauth2/authorize?client_id={{client id}}&response_type=code&redirect_uri=https://myurl.com/createpage/&response_mode=query&resource={{resource}}&state=9128',
crossDomain: true,
success: function(jsonData) {
alert(jsonData);
},
error: function(request, textStatus, errorThrown) {
console.log(errorThrown);
},
cache: false
});
But it is getting following error.
Jquery ajax error
Request you to help on resolving the issue.
You should check the content of the response in the tab network (in your dev tools).
I guess the response is not a valid json, so the parsing failed (your specified the jsonp type in your ajax call)

MS CRM 2013 adds version number to WebResources of script type

I have found strange issue in MS CRM 2013, and since it seems to be by design, I need help to find a way around it.
The issue is it's impossible to call getScript jQuery method from WebResource.
The CRM adds version string to the url, and this causes request fail with error 500.
For example, when I'm trying to call: /Organization/WebResources/Synchronization.js
The CRM turns this request into following: /Organization/WebResources/Synchronization.js?_=1402918931398 and it fails with server error 500.
Here is the sample code I'm using:
var settings = {
url: "/Organization/WebResources/Synchronization.js",
dataType: "script",
success: function (data) {
console.log("success");
},
error: function(jqXHR, textStatus, errorThrown) {
console.log("error");
}
};
$.ajax(settings);
Could you please point me, how I can find out when URL is changed?
It turns, that this is jQuery caching feature.
If caching will be turned on in settings object, the issue will disappear. Like this:
var settings = {
url: "/Organization/WebResources/Synchronization.js",
cache: true,
dataType: "script",
success: function (data) {
console.log("success");
},
error: function(jqXHR, textStatus, errorThrown) {
console.log("error");
}
};

Ajax call to JSON service isn't working

I'll admit my exposure to JQuery / Ajax has been somewhat limited thus far, I am attempting to get JSON data from two web services:
http://w.xaviertidus.com/Json.svc/getInServiceTransponders
http://w.xaviertidus.com/Json.svc/latestTransponderUpdates
Using the following code:
function fetchTransponderData() {
$.ajax({
url: "http://w.xaviertidus.com/Json.svc/getInServiceTransponders",
contentType: "application/json; charset=utf-8",
data: "{}",
dataType: "json",
success: function (response) {
return response;
},
error: function (xmlHttpRequest, textStatus, errorThrown) {
alert(errorThrown);
}
});
}
function fetchFarcsData() {
$.ajax({
url: "http://w.xaviertidus.com/Json.svc/latestTransponderUpdates",
contentType: "application/json; charset=utf-8",
data: "{}",
dataType: "json",
success: function (response) {
return response;
},
error: function (xmlHttpRequest, textStatus, errorThrown) {
alert(errorThrown);
}
});
}
Unfortunately it keeps throwing an error (the alerts under 'error' in the ajax statement fire) and not giving me detail, i have used fiddler and it is making a request to the webservices and going to them myself yields expected JSON results.
Can anyone shed any light on this problem for me? Many thanks!
Could be a cross domain request. If so you must set the appropriate header (Access-Control-Allow-Origin: *) or use a proxy server.
You can try using JSONP to get around the same origin policy. However, a better option would probably be to call the external site from your server side code and rely on your server to forward that result back to your client.

jQuery AJAX request on local development environment fail

I'm trying to test a jQuery/Javascript script on my local development environment. For some reason the script calls the error delegate. I've attempted to make sure I'm not performing a cross-domain request.
$.ajax({
url: 'http://localhost',
success: function(data, textStatus, jqXHR) {
console.log('success');
console.log(data);
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(textStatus);
console.log(errorThrown);
console.log(jqXHR);
}
});
return marker;
}
I'm running a WAMP stack. Here is my output:
error
undefined
XMLHttpRequest { mozResponseArrayBuffer=ArrayBuffer, status=0, more...}
Does anyone know what could be the issue?
Try replacing the absolute url:
url: 'http://localhost'
with a relative one:
url: '/'
If this doesn't solve your problem FireBug and/or Fiddler will give you more hints about what is going on and why the AJAX request fails.

Categories

Resources