How to call asp.net web services using ajax from cross domain

10,642

You need to also have Access-Control-Allow-Methods for the POST.

Also, you realize that using * for Origin allows anyone to access the service? You might want to use a library that's suited for implementing CORS, such as the Thinktecture IdentityModel library:

http://brockallen.com/2012/06/28/cors-support-in-webapi-mvc-and-iis-with-thinktecture-identitymodel/

Look into the IIS/Url option (since it seems you're hosting out of IIS).

Share:
10,642
Deepak Borade
Author by

Deepak Borade

Updated on June 04, 2022

Comments

  • Deepak Borade
    Deepak Borade almost 2 years

    i am developing mobile application using javascript,html,css(cross platform technology),i have written a web services using asp .net, i want to fetch the data from web services and display into client side using javascript/jquery. we point to web services and display the result but this is work in IE(internet explorer) only, we get the result as "true" response from server but in other browser (Mozilla,chrome) it does not work,we get result "false" as response from server.where as i am expecting result come as "true" in all the browser but its not happening.below i have given all the code that i have used.

    WebService.asmx.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Services;
    
    /// <summary>
    /// Summary description for WebService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class WebService : System.Web.Services.WebService {
    
        [WebMethod]
        public bool GetValue(string id,string pwd)
        {
            string userid = "abc";
            string password = "xyz";
            if (userid==id && password==pwd)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    
    }
    

    Web.config

    <?xml version="1.0"?>
    
    <configuration>
        <system.web>
            <compilation debug="true" targetFramework="4.0"/>
        </system.web>
        <system.webServer>
            <httpProtocol>
                <customHeaders>
                    <add name="Access-Control-Allow-Origin" value="*"/>
                    <add name="Access-Control-Allow-Headers" value="Content-Type"/>
                </customHeaders>
            </httpProtocol>
        </system.webServer>
    </configuration>
    

    HTML page Code

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>
        </title>
        <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
       <script>
           function JsonTest2() {
    jQuery.support.cors = true;
               $.ajax({
                   type: 'POST',
                   url: "http://10.16.10.35/webservice_test/WebService.asmx/GetValue",
                   data: '{"id":"vipul","pwd":"borole"}',
                   contentType: 'application/json; charset=UTF-8',
                   dataType: 'json',
                   async: false,
                   success: function (msg) {
                       alert(msg.d);
                   },
                   error: function (msg) {
                       alert('failure');
                       alert(msg);
                   }
               });
           }
       </script>
    </head>
    <body>
    <input id="Button1" type="button" value="button" onclick="javascript:JsonTest2();" />
    
    </body>
    </html>
    

    please help me to call this web service from all the browser i am not able to understand why it returning false