0

I am trying to upload a file from html 5 using wcf rest and jquery.its working fine in local.but not in server.kindly help me to solve this issue.

my js code is

function UploadFile() {
    var ajaxdata = new FormData();
    ajaxdata.append('action', 'col-xs-12 mid_forma');
    $.each($("input[type=file]"), function (i, obj) {
        $.each(obj.files, function (j, file) {
            ajaxdata.append('photo[' + i + ']', file);
        })
    });

    $.ajax({
        url: HostingInvestmentURL + 'UploadFileNew/',
        type: 'POST',
        data: ajaxdata,
        enctype: 'multipart/form-data',
        cache: false,
        dataType: 'json',
        processData: false,
        contentType: false,
        success: function (data) {
            //alert('Added Successfully');

        },
        error: function (data) {
            alert('Some error Occurred!');
        }
    });
}

my service is

[OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "/UploadFileNew", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]

I got the following error

XMLHttpRequest cannot load... The request was redirected to....
which is disallowed for cross-origin requests that require preflight.

kindly help me

Govinda Rajbhar
  • 2,926
  • 6
  • 37
  • 62
Raja
  • 1
  • 2

1 Answers1

0

When cross-domain access is enabled, the server should respond back to OPTIONS and allow the request to go through. This should return HTTP 200 unless there are other errors. But ajax callback will not be able to access the response. This can be done now only if Access-Control-Allow-Origin is set to "*" or the Origin request header value, and the Access-Control as needed. And the ajax callback will receive the response as intended.

Follow this link it may helpful to you What can I do to make this cross-domain request?

Community
  • 1
  • 1
Govinda Rajbhar
  • 2,926
  • 6
  • 37
  • 62
  • Thanks Raj for ur reply.but i have added these things in my Global.asax.cs file – Raja Jan 22 '16 at 11:23
  • protected void Application_BeginRequest(object sender, EventArgs e) { HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*"); if (HttpContext.Current.Request.HttpMethod == "OPTIONS") { HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST"); HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept"); HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000"); HttpContext.Current.Response.End(); – Raja Jan 22 '16 at 11:26