Getting cross-origin error - cross-domain

I am trying to make following ajax request:
$.ajax({
method: "GET",
url: http://google.com,
dataType: "json",
async: false,
cache: false,
success: function(data) {
alert("AJAX call successfully
completed");}});
But as expected, I am getting "Cross-Origin" error. Is there any way to bypass this security and make the ajax call without this error even if ajax call is being made from different domain.
Any response is appreciated.

You can use a browser plugin that will allow you to request any site with ajax from any source
Chrome example:
https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi

Related

call node.js file from ajax call in phonegap?

I want to call the node.js file through ajax call but unable to did, help me to understand which type of URL will be entered in ajax to call the node.js file.
If you are using Jquery on the client side
you can make the request this way :
$.ajax({
url: 'your-url',
success: function(data) {
//it works, do something with the data
},
error: function() {
//something went wrong, handle the error and display a message
}
});
You can read more details here PhoneGap and JSON API

Android Browser does not handle cross domain ajax request properly

I wrote HTML/JS application which works offline - it's put on SD card and is run using default 4.1 Android Browser.
I'm making the Ajax request using jQuery to host in the internet. Since i'm running it from local filesystem it's obviously a cross domain request. The server has a mechanism based on header manipulation which enables such CD requests. Piece of troubling code:
$.ajax({
type: "POST",
crossDomain: true,
data : formData,
cache: false,
url: "http://someurlwhichhandlescrossdomain.com",
success: function(data){
alert("hurrayy!");
},
error: function(xhr, ajaxOptions, thrownError) {
alert("bah :/");
alert(xhr.status);
alert(thrownError);
}
});
The problem is that the error function is always executed, although data is being sent correctly and the server registered the request properly. The xhr.status is always 0, and thrownError is empty.
The same piece of code works correctly on Chrome on Desktop (alerts "hurray").
Currently i use a workaround based on navigator.onLine, and if the value is true i assume that data has been sent correctly to the server (as i don't need to read anything from server). However this solution is bad because if the connection is working, but the server is not, i can't handle it.
Any ideas?
Thanks,
Karol

How to implement ajax in node.js to achieve partial postback

I am using node.js (express framework and hbs engine) and mongodb to develop an application.
I want to implement an ajax call to to page for partial post back.
As mentioned by others in comments, the best approach for this is described in Ajax to refresh a partial view using express and JQuery?
>*pass ur URL,and parameter to this function:It is working fine....*
function ajaxRequest(url, succesCallBck, completeCallBck, errorCallBck)
{
$.ajax({
url: url,
type: "POST",
dataType: "json",
complete: completeCallBck,
success: succesCallBck,
error: errorCallBck
});
}

node.js, express and different ports

I've been playing with node.js and then came across the express framework. I can't seem to get it to work when different ports are being used.
I have my ajax on http://localhost:8888 which is a MAMP server I'm running on my Mac.
$.ajax({
url: "http://localhost:1337/",
type: "GET",
dataType: "json",
data: { },
contentType: "application/json",
cache: false,
timeout: 5000,
success: function(data) {
alert(data);
},
error: function(jqXHR, textStatus, errorThrown) {
alert('error ' + textStatus + " " + errorThrown);
}
});
As you can see my node.js server is running on http://localhost:1337/. As a result nothing is getting returned and it's throwing an error.
Is there a way around this?
Thanks
Ben
The problem you have is that you are trying to make a cross-origin request and that's not allowed by browsers (yes, the same hostname with a different port counts as a different origin for this purpose). You have three options here:
1. Proxy the request.
Do this one if you can. Write some code that runs on the :8888 server
that proxies requests to the 1337 one. You can also do this by
sticking a proxy in front of both of them, something like Nginx is
pretty good at this and easy to set up
2. Use CORS (Cross Origin Resource Sharing)
See: http://en.wikipedia.org/wiki/Cross-origin_resource_sharing and https://developer.mozilla.org/en/http_access_control
This means adding some headers to your responses to tell the browser that cross-origin requests are ok here. In your Express server add middleware like this:
function enableCORSMiddleware (req,res,next) {
// You could use * instead of the url below to allow any origin,
// but be careful, you're opening yourself up to all sorts of things!
res.setHeader('Access-Control-Allow-Origin', "http://localhost:8888");
next()
}
server.use(enableCORSMiddleware);
3. Use JSONP
This is a trick where you encode your "AJAX" response as Javascript code. Then you ask the browser to load that code, the browser will happily load scripts cross-origin so this gets round the cross-origin issue. It also lets anyone else get round it as well though, so be sure that's what you want!
On the server side you need wrap your response in a Javascript function call, express can do this for automatically if you enable the "jsonp callback" option like this:
server.enable("jsonp callback");
Then send your response using the "json()" method of response:
server.get("/ajax", function(req, res) {
res.json({foo: "bar"});
});
On the client side you can enanble JSONP in jQuery just by changing "json" to "jsonp" in the dataType option:
dataType: "jsonp",

Addressing relative paths within Google Chrome extension

in my Google Chrome extension I'm trying to do an AJAX request using jQuery. This is the code I use:
//send data to the server
$.ajax({
type: "PUT",
url: "/addTransaction",
data: receivedData,
success: function(data){
console.log(data);
}
});
The problem I have is that Chrome tries to access to something like this chrome-extension://jeelhjnbimpcilllldencicbafjdgdon/addTransaction which gives of course an error. I don't want to put absolute paths because it resets the sessions in the server (node.js).
Any clue on how I can solve this? Thanks.

Resources