Click here to Skip to main content
15,896,304 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I am trying to get data from asp.net webservice to html.
Initially i have pre published webservice in my localhost, it returns a json formated string.
While retrieving json data from webservice, the browsers blocks response and shows a message:--->

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:55619/WebService_DataCounter.asmx/Indus_Gadget_Data. This can be fixed by moving the resource to the same domain or enabling CORS. Indus_Gadget_Data.....

here is my code in webservice
C#
[WebMethod]
      public DataTable Indus_Gadget_Data()
      {
          dt = new DataTable();
          dt = process_indus_gadget_data.Indus_Gadget_Processing();
          dt.TableName = "All Sites data";
          System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
          List<overallstatics> details = new List<overallstatics>();
          details.Clear();
          if (dt.Rows.Count > 0 && dt != null)
          {
              foreach (DataRow dr in dt.Rows)
              {
                  OverallStatics user = new OverallStatics();
                  user.SiteName = dr[0].ToString();
                  user.EB = dr[1].ToString();
                  user.DG = dr[2].ToString();
                  user.LS = dr[3].ToString();
                  user.DC = dr[4].ToString();
                  details.Add(user);
              }
          }
         return serializer.Serialize(details);
      }

now in html data retrieving
HTML
<script type="text/javascript">
     document.onload = init();
     function init() {
         Get_Indus_Widget();
     }
     function Get_Indus_Widget() {
         $.ajax({
             type: "POST",
             url: "http://192.168.0.108:55619/WebService_DataCounter.asmx/Indus_Gadget_Data",
         // url: "WebService1.asmx/Indus_Gadget_Data",
             data: "{}",
             headers: { 'Access-Control-Allow-Origin': '*' },
             crossDomain: true,
             contentType: "application/json",
             dataType: "json",
             beforeSend: function (xhr) {
                 xhr.setRequestHeader("Access-Control-Allow-Origin", "*");
             },
             headers: {
                 'Access-Control-Allow-Origin': '*'},
            // jsonp: "mycallback",
             //crossDomain: true,
            // jsonpCallback: 'jsonCallback',
             success: function (data) {
                 for (var i = 0; i < data.d.length; i++) {
                     $("#tbDetails").append("<tr><td>" + data.d[i].SiteName + "</td><td>" + data.d[i].EB + "</td><td>" + data.d[i].DG + "</td><td>" + data.d[i].LS + "</td><td>" + data.d[i].DC + "</td></tr>");
                 }
             },
             error: function (result) {
                 alert("Error");
             }
         });
     }


 </script>
Posted
Updated 24-Dec-14 23:56pm
v2
Comments
Ambati Dilip 25-Dec-14 5:49am    
Here i am not going to use web.config. Because i am creating a windows gadget, it requires only css, html, xml
Krishna Chaitanya Kollu 25-Dec-14 5:52am    
are you telugu
Ambati Dilip 25-Dec-14 23:42pm    
Yes
Krishna Chaitanya Kollu 25-Dec-14 23:47pm    
http://www.codeproject.com/Questions/856895/How-to-Retrive-The-Particular-Book-Details-From-Go
I Had a Problem Please Check if you can resolve this

1 solution

Hello I Have Write Code For Retrive a Code

You have parse json Data like Which is Bold Line

XML
$.ajax({
                  type: "POST",
                  contentType: "application/json; charset=utf-8",
                  url: "controls/Ajax.aspx/Bindcombo",
                  data: "{datatype:'" + name + "'}",
                  success: function (data) {
                      var list = $.parseJSON(data.d);

                     

                    }
                });
 
Share this answer
 
Comments
Ambati Dilip 26-Dec-14 0:12am    
I am getting the same error
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:55619/WebService_DataCounter.asmx/Indus_Gadget_Data. This can be fixed by moving the resource to the same domain or enabling CORS. Indus_Gadget_Data.....
Ambati Dilip 26-Dec-14 0:19am    
XMLHttpRequest cannot load http://localhost:55619/WebService_DataCounter.asmx/Indus_Gadget_Data. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:56708' is therefore not allowed access.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900