locked
How to call webService from external webApp via ajax request? RRS feed

  • Question

  • User-79977429 posted

    Hi

    in my app, i have asmx page which contains my sample webService and call from my page via this code :

    $.ajax({
                    type: "POST",
                    crossDomain: true,
                    contentType: "application/json; charset=utf-8",
                    url: 'myLocalPage.asmx/GetText',
                    data: '{}',
                    dataType: "json",
                    async: false,
                    success: function (data) {
                        alert('ok');
                    },
                    failure: function (response) {
                        alert('fail');
                    },
                    error: function (request, status, error) {
                        alert(request.responseText);
                    }
                });

    It works fine, but when i change the url parameter as follow, the error section occures :

    $.ajax({
                    type: "POST",
                    crossDomain: true,
                    contentType: "application/json; charset=utf-8",
                    url: 'http://localhost:5473/myExternalPage.asmx/GetText',
                    data: '{}',
                    dataType: "json",
                    async: false,
                    success: function (data) {
                        alert('ok');
                    },
                    failure: function (response) {
                        alert('fail');
                    },
                    error: function (request, status, error) {
                        alert(request.responseText);
                    }
                });

    Even i've changed dataType from json to jsonp but i'm facing the same problem!

    How to solve this problem ?

    thanks in advance

    Saturday, January 5, 2019 1:13 PM

Answers

  • User475983607 posted

    NetworkError: A network error occurred.

    Did you start the second web application; ctrl-F5?  Also did you enable CORS if this is a second application?

    https://www.c-sharpcorner.com/forums/how-to-enable-cors-in-asp-net-web-method

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, January 5, 2019 2:11 PM
  • User-79977429 posted

    Also did you enable CORS if this is a second application?

    Thanks mgebhard, i've add this section in my external webService project web.config :

    <system.webServer>
          <httpProtocol>
            <customHeaders>
              <add name="Access-Control-Allow-Headers" value="accept, content-type" />
              <add name="Access-Control-Allow-Origin" value="*" />
              <add name="Access-Control-Allow-Methods" value="POST, GET, OPTIONS" />
            </customHeaders>
          </httpProtocol>
        </system.webServer>

    And now it;s working!!

    Thanks again.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, January 5, 2019 2:19 PM

All replies

  • User475983607 posted
    What is the error?
    Saturday, January 5, 2019 1:18 PM
  • User-79977429 posted

    NetworkError: A network error occurred.

    Saturday, January 5, 2019 2:08 PM
  • User475983607 posted

    NetworkError: A network error occurred.

    Did you start the second web application; ctrl-F5?  Also did you enable CORS if this is a second application?

    https://www.c-sharpcorner.com/forums/how-to-enable-cors-in-asp-net-web-method

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, January 5, 2019 2:11 PM
  • User-79977429 posted

    Also did you enable CORS if this is a second application?

    Thanks mgebhard, i've add this section in my external webService project web.config :

    <system.webServer>
          <httpProtocol>
            <customHeaders>
              <add name="Access-Control-Allow-Headers" value="accept, content-type" />
              <add name="Access-Control-Allow-Origin" value="*" />
              <add name="Access-Control-Allow-Methods" value="POST, GET, OPTIONS" />
            </customHeaders>
          </httpProtocol>
        </system.webServer>

    And now it;s working!!

    Thanks again.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, January 5, 2019 2:19 PM