0

i am unable to consuming web service using jquery ajax but it is possible using C# language. This is my jquery Ajax code`

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="jquery-3.0.0.js"></script>
    <script>
        $(document).ready(function(){


            var url = "http://192.xxxx.xx.xx:10000/service1.asmx/GetJsonData";

            $.ajax({
                type: "GET",

                url: url,

                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    alert("hii");
                    alert(data.d)
                },
                error: function (xmlHttpRequest, textStatus, errorThrown) {
                    alert("error");
                }
            });


        })
    </script>
</head>
<body>
    <form id="form1"  runat="server">
    <div>

    </div>
    </form>
</body>
</html>

The above code is not working but when i am trying the same using  C#  language it is working fine.

Error is:Network error:the Acess is denied in IE browser.

And the Error In Google Chrome is like following. XMLHttpRequest cannot load http://192.xxxx.xx.xx:10000/service1.asmx/GetJsonData.No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:49951' is therefore not allowed access.

`
GOPAL YADAV
  • 1,181
  • 2
  • 9
  • 18

2 Answers2

0

Aparently it is an Ajax issue, not related to C#. Take a look here: "No 'Access-Control-Allow-Origin' header is present on the requested resource"

Community
  • 1
  • 1
thcerutti
  • 81
  • 2
0

Possible Issue

Seems to be a CORS related issue.

From the link:

For security reasons, browsers restrict cross-origin HTTP requests initiated from within scripts.

This happens when you try to access a resource of one domain from another domain. Example: a script within twitter.com trying to access a facebook.com API endpoint.


Possible Solution(s)

If this is the case:

  • If you're in control of the server, you can set the Access-Control-Allow-Origin as appropriate.
  • If you do not control the server, you can see if the web application serving the requests has any approaches to allow requests from other domains
    • Setting appropriate Access-Control-Allow-Origin, according to the OAuth token supplied
    • Having a page from the target web application loaded in an IFrame, which will make the requests on your behalf. Communicating with the IFrame using browser's messaging API.
Fa773N M0nK
  • 120
  • 1
  • 2
  • 9
  • hi @Fa773N, i don't have acess the server, tell me the process where i am done the settings in My application.like in eb.config or in My aspx.cs code. – GOPAL YADAV Jul 06 '16 at 12:22
  • @GOPALYADAV: I'm not familiar with the asmx technology. A quick search got me [this question](http://stackoverflow.com/questions/1309767/adding-custom-http-headers-to-web-service-proxy) though. Do try and see if you can set the header using this method. – Fa773N M0nK Jul 06 '16 at 15:09