My Rails application is accessing MongoLab DB that has data loaded to it.
I am making a AJAX request to my DB. Here is the code:
$.ajax({
type: "GET",
url: "mongodb://heroku_rgkq33fl:kbi2jctr39ts8gicrrchjuso5s#ds061954.mongolab.com:61954/heroku_rgkq33fl",
success: function(data){
console.log("Success 1234");
console.log(data);
},
error: function(data)
{
console.log("Error");
}
})
}
I am not able to make a AJAX request because of the following error:
XMLHttpRequest cannot load mongodb://heroku_rgkq33fl:kbi2jctr39ts8gicrrchjuso5s#ds061954.mongolab.com:61954/heroku_rgkq33fl. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.
Can someone please help me on how to get the data from my DataBase to my html page.
Related
From my work gave me a project to finish with which I am having the following problem:
In solution there is web API and RESTful API in MVC
if I call directly Restful service it's responding
if i make a test and put in home controller in web API HttpWebResponse it is responding
but the request is put in .js file and the code is
return $.ajax({
type: 'GET',
url: serviceURL,
contentType: "application/json; charset=utf-8",
dataType: "json",
data: urlParams,
crossDomain: true,
beforeSend: lazy(),
complete: complete()
});
and this is failing with the following message e requested resource does not support http method 'OPTIONS
in webconfig everything is settled correctly
It is not working if I call REST API locally, if I call those which is on server it is working.
I read almost everything connected to that problem, but I am still stuck.
I am having real difficulty knowing what to do. I have been reading some sources that are inferring that you cannot make ajax calls when launching an html file from the local file system (file:// in the url).
I have a html file on my desktop (ie in the browser url file://) containing the following ajax call:
$.ajax({
type: "POST", // This for array
url: "http://www.mywebsite.com/exclude.php",
async: true,
cache: false,
crossDomain: true, // This needs to be true for other people
success: function (data) {
var json = jQuery.parseJSON(data);
// Use json in interesting ways
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
// Silent error
}
});
My php file on my server is basic:
<?php
header("Access-Control-Allow-Origin: *"); // To allow cross domain
$KeyCodeStatics_BlackList = ["181K2", "4V419"];
echo json_encode($KeyCodeStatics_BlackList);
?>
Some are suggesting ajax calls are not allowed from an html file launched in the local file system (file:// in url).
The errors I am getting:
Origin file: not found in Access-Control-Allow-Origin header.
and
XMLHttpRequest: Network Error 0x80700013, Could not complete the operation due to error 80700013.
How can I overcome this? I need to make an async call to /www.mywebsite.com/exclude.php from the html file from my local file system.
Note: It is not just me that has to do it, it is every end-user that uses this html file. So this means I cannot ask them to change security settings on their browser.
Here is my little first project as practice, having real trouble figuring out how to use JSON, took me a while to get it work locally but still no luck with servers, and tried few a including one i hosted, and even tried it with other hosted json files.
http://jsfiddle.net/Atlas_/Mgyc5/1/
$.ajax({
dataType: "json",
async: false,
url: "package.json", //https://www.dropbox.com/s/fmw63i4v7dtnx6t/package.json
'success': function (json) {
theQuiz = json.quiz;
console.log(json);
console.log(theQuiz);
}
});
When you access another domain be carefully with "Crossdomain".
To use with dropbox, try changing the URL to:
https://dl.dropboxusercontent.com/s/fmw63i4v7dtnx6t/package.json
Your code will be like this:
$.ajax({
dataType: "json",
async: false,
url: "https://dl.dropboxusercontent.com/s/fmw63i4v7dtnx6t/package.json",
success: function (json) {
theQuiz = json.quiz;
console.log(json);
console.log(theQuiz);
}
});
When you request 'dl.dropboxusercontent.com', you have this:
Access-Control-Allow-Origin: *
https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS?redirectlocale=en-US&redirectslug=HTTP_access_control
"In this case, the server responds with a Access-Control-Allow-Origin:
* which means that the resource can be accessed by any domain in a cross-site manner."
Another options: Some websites (twitter, for example) work with "jsonp". http://en.wikipedia.org/wiki/JSONP or ..you can create your own proxy.
solved - it was Adblock Plus
I have a cross-domain OAuth scenario where I'm calling OAuth protected jsonp resources on a 3rd party api directly from JavaScript.
The client relies on my server (the site of origin) to generate signed request urls for OAuth resources on the provider. The client GETs these OAuth resources directly in an attempt to limit OAuth proxy duties of my server to simply generating signed resource urls and letting JS do the rest. Here's the JS:
myNamespace = {
callApi: function(signedRequest, callback) {
$.ajax({
url: signedRequest,
dataType: "jsonp",
type: "GET",
crossDomain: true,
jsonp: false,
jsonpCallback: callback,
cache: true,
async: true
});
},
usersCallback: function(jsonp) {
alert(jsonp);
},
getOAuthResource: function(resource, callback) {
$.ajax({
url: "/GetSignedRequest?resource=" + encodeURIComponent(resource + "&callback=" + callback),
success: function (signedRequest) {
myNamespace.callApi(signedRequest, callback);
}
});
}
}
Because the parameter order matters when signing the request, I include the callback manually so that it gets ordered & included in the signed request the server generates.
So an example usage would be simply,
myNamespace.getOAuthResource("/users?id=1", "myNamespace.usersCallback");
This is working in FireFox 20 and IE 10 - I get the alert msg & the expected json from the provider.
Chrome 26 refuses to perform the cross-domain call to the provider. In dev tools, the GET for the cross-domain resource immediately shows a Status of "(failed)" and the GET shows up in the console as an error.
If I subsequently ask Chrome to open this supposedly "failed" URL in a new window, it re-requests the URL directly on the provider and I get the expected jsonp response:
myNamespace.usersCallback({...})
Why won't Chrome perform the cross-domain call?
I'm developing an authentication website that authenticate data from a web service. My website is running locally and the web service is running on different domain ex: test.abc.com. if authentication is success then store the response in cookies.
if the data is available in cookies and not expired then in the second call do not ask for authentication but need to validate the user from back end. For that i am using below code.
$.ajax({
url:"https://test.abc.com/test/DummyTest",
method:"GET",
dataType:"json",
beforeSend: function(xhr){
xhr.withCredentials = true;
},
success:function(data){
alert(Success);
},
error:function(xhr,err){
alert("Error");
});
I am using jquery1.6
I have checked the browser Options and the cookies are stored with my localhost ip(160.225.230.50) address. but the web service is in different domain(abc.com). while accessing the second time, I got Error response.
Please help out me on this.
For this to work your auth page should send Access-Control-Allow-Origin and Access-Control-Allow-Credentials headers.
When using Access-Control-Allow-Credentials: true you cannot set Access-Control-Allow-Origin to wildcard, but need to specify origin: Access-Control-Allow-Origin: https://abc.com
Also, there is a simpler way to set xhr fields:
$.ajax({
xhrFields: {
withCredentials: true
},
type: 'POST',
url: ...
});