0

I'm Getting

XMLHttpRequest cannot load http://myurl.com/RestServiceImpl.svc/post. Origin http://myurl2.com is not allowed by Access-Control-Allow-Origin. This error in my client Javascript app While calling a Post service written in WCF Rest

My Service

[OperationContract]
        [WebInvoke(Method = "POST",
            ResponseFormat = WebMessageFormat.Json,
            RequestFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.Wrapped,
            UriTemplate = "post")]
        bool JSONDataPost(string Value);


public bool JSONDataPost(string Value)
        {   //code to Create the Person goes here

            return true;
        }

And my Client request is

var xhr = new XMLHttpRequest();
    xhr.open('POST', 'http://myurl.com/RestServiceImpl.svc/post', true);
    xhr.setRequestHeader("Content-Type", "application/jsonp;charset=UTF-8");
    xhr.onload = function () {
        // do something to response

    console.log(this.responseText);
    };
    xhr.send(JSON.stringify("TEST"));
  • You need to get `http://myurl.com/RestServiceImpl.svc/post` to respond with the `CORS` headers when you send a `OPTIONS` verb request. I recommend you switch to `ASP.Net Web-API` as you have much better control over REST verb responses. http://stackoverflow.com/questions/7234599/cors-support-within-wcf-rest-services – Aron Oct 15 '13 at 04:44
  • Also, `application/jsonp` is not a valid content-type and `"TEST"` is not valid JSON. You should use `application/json` and `{ "TEST": "Hello World!" }` respectively. – idbehold Oct 15 '13 at 19:22

0 Answers0