Headers Issue with file upload via CarrierWave - ajax

I am trying to upload a file via Carrierwave to a Rails backend with this Ajax post
$.ajax({
url: mypath
data: myformdata
contentType: false
processData: false
type: "POST"
})
I am getting a "No 'Access-Control-Allow-Origin' header is present on the request resource" and therefore a 500 Internal Server Error.
All other requests are handled by Ember.js and are working properly, through simple-auth-devise.
Any idea what the problem might be?
Thank you in advance for any suggestion!

You need to setup your app to work with CORS on your Rails backend.
See these articles:
http://leopard.in.ua/2012/07/08/using-cors-with-rails/
https://demisx.github.io/rails-api/2014/02/18/configure-accept-headers-cors.html

Related

Cross Origin Domain Blocking error with AJAX

Could anyone please tell me what I am doing wrong:
I'm using the following code and getting a Cross Origin Domain Blocking error. And no I don't have the ability to enable CORS on the Server Im getting data from. But when I use the URL by itself, I get the data I am after, it's just not working with AJAX, Im trying to use JSONP to get around the issue:
$(document).ready(function () {
$.ajax({
url: "https://serveraddress/remote-json.cfm?do=gettimetable",
dataType: "jsonp",
jsonpCallback: "logResults"
});
});
kind regards,
Brad
You can use back-end to solve this problem. This back-end(php, java, nodejs, curl...) implement a httpclient to getting data from any server.
I try to set "useDefaultXhrHeader: false", it is the configuration ajax.

JavaScript: cross site request on localhost on ASP.NET WebAPI

I have an ASP.NET WebAPI application running on
http://localhost:13057/worldwind
I have another ASP.NET MVC5 application running on
http://localhost:2425/worldwind
When the client code on the MVC5 application attempts the following AJAX call:
$.ajax(http://localhost:13057/worldwind/images,
{
type: "GET",
contentType: "application/json; charset=utf-8",
dataType: "json",
error: Utilities.Logger.displayAjaxError,
success: this.onImagesRetrieved,
context: this
});
I get error:
XMLHttpRequest cannot load http://localhost:13057/worldwind/api/images. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:2425' is therefore not allowed access.
If I change the dataType in the AJAX request to jsonp, the WebAPI is being invoked even though I get error
jQuery18209344927002675831_1412452833242 was not called
How do I get this thing working?
I ended up enabling CORS following instructions from here
This involved 3 steps (well, after fighting with other NuGet upgrade issues):
Adding NuGet package Microsoft.AspNet.WebApi.Cors
Adding the following lines in WebApiCconfig.Register static method:
var cors = new EnableCorsAttribute("http://locahost:2425", "*", "*");
config.EnableCors(cors);
Rebuilding and running.
if your .jsp put in
http://localhost:13057/MySite/JSONP.jsp
use your script section like this:
<script type = "text/javascript" src="http://localhost:13057/MySite/JSONP.jsp?callback=jsonHandler"></script>
hope it will be help.

jQuery ajax POST from local file to access a cross domain not working

As the title says, I'm trying to access (POST) using jQuery AJAX call to a web url, http://host:port/... or http://localhost:8080/... from a local HTML file, c:\home.html. I can't get it to work.
I did Google and also saw several questions here but I can't get it to work. I need some help here. Here is what I've tried so far.
dataType: jsonp
crossDomain: true
Setting the header in my response:
response.setHeader("Access-Control-Allow-Origin", "*");
None of the three browsers are working - IE, FF or Chrome. The request is never reaching the server. Here are some of the errors I'm seeing.
No Transport (IE) if not jsonp is used.
NS_BINDING_ABORTED / Error loading content (NS_ERROR_DOCUMENT_NOT_CACHED) in FF
This is my code. I would appreciate any help. I'm using jquery-1.8.2.min.js.
var http_host = "http://localhost:8080";
function su (pc, p) {
var suUrl = http_host + "/ps/api/v2/authorize.json";
$.ajax({
type: 'POST',
url: suUrl,
data: {
phone_cell: pc,
password: p,
},
dataType: "json",
crossDomain: true,
success: osu,
error: oe
});
return false;
}
function osu (d) {
console.log(d);
}
function oe(xhr, ts, et) {
alert("ServerError: " + et);
}
An example would be a perfect pointer.
I suppose my code got messed up w/ all the different solutions that I was trying. I was finally able to get it to work w/ setting the header (solution that was recommended and worked for others). All that I had to do to get it to work is add the following to my REST service response.
response.setHeader("Access-Control-Allow-Origin", "*");
Update:
I thought I figured this out but I've not. There is more it than just setting the header. Anyways, in my specific situation. I was trying to run my app (html, js) off of the hard drive specifically on chrome and trying to access web services available on the cloud.
Here is how I finally solved the problem. I started the chrome w/ the following parameters.
--disable-web-security -–allow-file-access-from-files
Like I mentioned earlier, this app is really a desktop application that will be run as part of the chromium embedded framework.
Thanks every one for your input.
You can't make a cross-domain request from a local file because it's not on a domain. You need to host C:\home.html on a local webserver instance in order for it to work.

$.ajax with JSONP causes IE8 to throw security warning

I have a https site. I'm running LifeRay on Tomcat. I'm using the following URL:
http://gdata.youtube.com/feeds/api/videos/ID?v=2&alt=jsonc
and
jQuery.ajax({
url: URL,
dataType: 'jsonp',
async: false,
success: function (obj) {
processData(obj);
}
});
to get the data and then processing it. It works on all the browsers. The only problem is that I get a security warning in IE8.
Question 1: Is there any way to get the JSON data securely and processing the data without IE throwing any warning messages?
Question 2: How and where can I set this: Access-Control-Allow-Origin: http://youtube.com, so that maybe IE won't throw any warning messages?
Try using
https://gdata.youtube.com/feeds/api/videos/ID?v=2&alt=jsonc
Since you are in https, IE wants all resources accessible within that domain to be secured. I feel previously you were using http:// in the request URL of youtube. Change it https, it may solve the security warning issue.
If a secure page loads any nonsecure resource, its going to throw the warning. The only way to get around it is to load everything from https.
Here you are trying to load a non secured resource (http://gdata.youtube....) in the secured website.
HTH

How to get/post/delete/put information with jQuery and AJAX

I trying to do a DELETE, PUT, GET and POST a request with ajax and jquery.
The method POST works well by creating a new record, but I cannot make it work the other methods (PUT, DELETE and GET).
This is the code (it works fine, it creates the new record but it doesn't reach the "success" event):
var jsonExample = {"advertisement":{"title":"test"}};
$.ajax({
type: "POST",
url: "http://example.com/advertisements.json",
data:jsonExample,
success: function(response){
alert("test");
}
});
When I change the type "POST" to "DELETE" or "PUT" I have the follow error:
NetworkError: 404 Not Found
And when I change it to "GET" it throws the following message:
200 OK
But it don't any other responses. It should be something like this:
{"advertisement":{"created_at":"2012-04-17T13:20:17Z","from_age":null,"neighbourhood_id":null,"title":null,"date_to":null,"days":null,"promotion_id":null,"updated_at":"2012-04-17T13:20:17Z","date_from":null,"gender":null,"id":3,"display":null,"desc":null,"budget":null,"image":null,"to_age":null,"department_id":null,"town_id":null}}
The
Please note: my app is getting this info from a remote server, but I don't know if that has something to do with this problem. Because I've run it in Google Chrome and I've received the Access-Control-Allow-Origin message on the browser's console.
Any ideas?
You cannot make cross-domain AJAX requests using jQuery for security reasons. You may however be able to use jsonp providing that the URL you are requesting the data from is set up to handle jsonp requests.
This article should help you out alot more than I'm able to: http://www.fbloggs.com/2010/07/09/how-to-access-cross-domain-data-with-ajax-using-jsonp-jquery-and-php/

Resources