-1

I have a WebAPI and I need AJAX call to access this but on the cross domain

Here is my code :

$.ajax({
         type: "POST",
         url: '/api/myapiname/apiactionname',
         contentType: "application/json",
         data: {
                name: "Abhishek",
                email: "abhi@abhi.com",
                password: "******"
            },
         crossDomain:true, 
         success: function(data) { console.log(data); },
         error: function(data) {console.log(data); },
         dataType: 'json',
         beforeSend: function (xhr) {
             xhr.setRequestHeader("Access-Control-Allow-Origin", "*");
         },

    });
  • 2
    And the problem is...? What's your question? – Chris Pratt Feb 25 '16 at 14:41
  • 1
    Possible duplicates: [jQuery AJAX cross domain](http://stackoverflow.com/questions/3506208/jquery-ajax-cross-domain) and [Make cross-domain ajax JSONP request with jQuery](http://stackoverflow.com/questions/11736431/make-cross-domain-ajax-jsonp-request-with-jquery/11736771#11736771) – Igor Feb 25 '16 at 14:42
  • @Igor: Not a true dupe. Those solutions could work, but you don't *have* to use JSONP, if everything is set up correctly. Problem is we don't yet know what's wrong or how things are set up on the OP's side. – Chris Pratt Feb 25 '16 at 14:43
  • well chris , i am unable to call the API through this code – Abhishek Singh Mar 02 '16 at 11:41

1 Answers1

1

Have you enabled CORS within your API? Follow the link, http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api

Rav
  • 703
  • 4
  • 13