-1

I am trying to get some return value from Web Service and i am using ajax jQuery.

I am getting these errors:

XMLHttpRequest cannot load 'http....'. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8181' is therefore not allowed access

XHR failed loading: GET "http...".

<script language="text/javascript">
  getUserValue();

  function getUserValue() {
    var number = "0000317930";
    var fullName = "NURULLAH ALTINTAŞ";
    var dataString = "{ 'number' : '" + number + "', 'fullName' : '" + fullName + "'}";
    $.ajax({
      type: "GET",
      url: "http://...",
      data: {
        number: number,
        fullName: fullName
      },
      contentType: "application/x-www-form-urlencoded; charset=UTF-8",
      dataType: "xml",
      success: OnSuccessGetConnectionArticles,
      failure: OnFailureGetConnectionArticles,
      error: OnErrorGetConnectionArticles
    });
  }

  function OnSuccessGetConnectionArticles(response) {
    debugger;
    $.each(response.RestResponse.result, function(index, value) {
      $("#list").append('<li><span class="tab">' + value.name + '</span></li>');
    });
  }

  function OnErrorGetConnectionArticles(response) {
    debugger;
    alert(response.d);
  }

  function OnFailureGetConnectionArticles(response) {
    debugger;
    alert(response.d);
  }
</script>

enter image description here

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Nagikho
  • 83
  • 1
  • 1
  • 9
  • 2
    Are you trying to make AJAX requests across different servers? That's not directly allowed for security reasons. You can look into [CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS) as a possible solution but that does require the server and client to alter their requests to support the protocol. – MrFlick Feb 16 '17 at 14:57
  • Yes i am, thank you. – Nagikho Feb 16 '17 at 14:59
  • There are lots of content about that. Please use the search bar – Kamuran Sönecek Feb 16 '17 at 15:03
  • Yeah i can see but i am not using php or asp.net, i know what i have to do when i use them but i am trying on html page, if i solve this i will use in Phonegap for making hybrid application. – Nagikho Feb 16 '17 at 15:11

2 Answers2

1

If you are using PHP in server side you should use in your requested page the following code::

$origin = 'http://localhost:8181';
header("Access-Control-Allow-Origin: " . $origin);

if you are using some other anguage then you should find similar to this.

Rana Ghosh
  • 4,514
  • 5
  • 23
  • 40
0

I think you should add these lines to your web.config file for Allow-Origin error.

<system.webServer>
  <httpProtocol>
    <customHeaders>
      <add name="Access-Control-Allow-Origin" value="*" />
      <add name="Access-Control-Allow-Headers" value="Content-Type" />
      <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
    </customHeaders>
  </httpProtocol>
</system.webServer>
Harun KARATAŞ
  • 116
  • 2
  • 11