Ajax call to JSON service isn't working - javascript

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.

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)

Ajax jquery call to GET application/x-javascript

How do I get content of "application/x-javascript" using jquery Ajax call?
As it keep getting me null content.
What I am trying to use for now:
$.ajax({
dataType: "json",
contentType: "application/x-javascript;charset=utf-8",
url:the_url,
async:false,
success:function(r){
console.log("el result" + r) ;
response = r;
}
});
This:
dataType: "json",
tells jQuery to ignore what the server claims it is sending back and to process the result as JSON. JavaScript isn't JSON, so this breaks it.
Remove that line.
Then you should get the data in the success function.
Asides:
This:
contentType: "application/x-javascript;charset=utf-8",
claims you are sending JavaScript. You aren't making a POST request, so you aren't sending anything. Remove it.
Even if you were sending JavaScript to the server, the application/javascript MIME type hasn't been experimental since 2006, so it shouldn't have the x- prefix on it.
async:false, is a terrible idea. It locks up the JS event loop waiting for the response. You shouldn't use it.
response = r;: assigning data to globals is usually a terrible idea. Process the data in the success event handler instead.
Try this out :
$.ajax({
url: 'my/url',
type: 'GET',
data: 'test=mytest&test2=mytest2',
success: function (data) {
console.log(data);
},
error: function (jqXHR, textStatus, errorThrown) {
alert(errorThrown);
}
});
And read the documentation, to see what each parameter is made for :
http://api.jquery.com/jquery.ajax/

How do I "print" from PhP in a cross-domain sort of way?

I'm developing a mobile application and am having trouble debugging my server-side code. I make cross-domain AJAX requests from the app. Normally, I would use var_dump() to display the contents of variables to the browser, but with these cross-domain requests, I can't figure out how to do something similar. What are some common methods that one might use to visualize what happens within the server-side script from a cross-domain perspective?
Here is the bulk of what my AJAX requests look like:
$.ajax({
async: false,
url: 'http://mysecretsite.com/page.php',
data: mySecretData,
dataType: 'jsonp',
jsonp: 'callback',
jsonpCallback: 'mySecretCallback',
success: function(){
alert("success");
},
error: function(xhr, status, errorThrown){
alert("Oops: " + status);
},
complete: function(){
alert("complete");
}
});

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");
}
};

Categories

Resources