0

I have the following controller defined in a web api project

    public IEnumerable<Speciality> GetAllSpecialities()
    {
        List<Speciality> specialities = null;

        try
        {
            specialities = (new Datatable(Properties.Settings.Default.DataConnection)).Select<Speciality>(null);

        }
        catch ( System.Exception ex)
        {
            throw ex;
        }
        finally
        {

        }

        return specialities;
    }

    public IHttpActionResult GetSpeciality(String id)
    {
        Speciality speciality = null;

        try
        {
            speciality = DALObject.Get<Speciality>(Properties.Settings.Default.DataConnection,
                new Dictionary<string, object>()
                {
                    { "SpecialityCode",id }

                },null);

            if (speciality == null)
            {
                return NotFound();
            }
        }
        catch ( System.Exception ex)
        {
            throw ex;
        }

        return Ok(speciality);

    }

If I call the web service from the browser they both return a JSON object back, the first a list of specialties and the second an individual one.

In my page I have the following ajax code

    $.ajax({
        url: 'http://stg1edmapps01:9090/api/specialties',
        contentType:'json',
        success: function (result) {
            alert('data collected successfully');
        },
        error: function (request, status, error) {
            alert('something is broken');
            alert('Response ' + error);
        }
    });

Here I get three alerts;

The first is the one showing it calling the code The second says 'something is broken'

The third however just gives the word 'Response' unless that is I pass error to the final alert in which case it gives the string 'Response error'.

What have I done wrong in that I was expecting to get a JSON object with a list of specialties. The same happens regardless of passing api/specialties/100 or just api/specialties

Paul S Chapman
  • 832
  • 10
  • 37
  • If I have understood correctly that is done in the webapiconfig, anyway if I make the following url call in the browser address bar, http://stg1edmapps01:9090/api/specialties since the api is installed on the stg1edmapps server it does return a json object – Paul S Chapman Mar 09 '20 at 16:46

2 Answers2

1

The best way to know what is happening in this case is to make a console.log (request + status + error); and get more information I also see that in the return do return NotFound(); but I don't see it coming back.

As a last option you can try to make the call with fetch ( it doesn't work with internet explorer) https://developer.mozilla.org/es/docs/Web/API/Response/status

  • RoutingManagement.html:1 Access to XMLHttpRequest at 'http://stg1edmapps01:9090/api/specialties' from origin 'http://localhost:56355' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. RoutingManagement.html:102 [object Object]error jquery.min.js:18 GET http://stg1edmapps01:9090/api/specialties net::ERR_FAILED send @ jquery.min.js:18 ajax @ jquery.min.js:18 (anonymous) @ RoutingManagement.html:94 – Paul S Chapman Mar 09 '20 at 16:56
  • these two links can serve you I have no more experience with domain crossings --> [Access_control_CORS](https://developer.mozilla.org/es/docs/Web/HTTP/Access_control_CORS) and --> [has-been-blocked-by-cors-policy-response-to-preflight-request-doesn-t-pass-acce](https://stackoverflow.com/questions/53298478/has-been-blocked-by-cors-policy-response-to-preflight-request-doesn-t-pass-acce) – Enrique Asensio Mar 09 '20 at 17:10
0

Enrique's answer gave me the means to find out what the problem is which is down to cross-domain issues, and CORS policy.

As the whole purpose of this design is for a common library this is going to be an issue and the library needs to provide the relevant header information to the calling application.

In order to do this first use the NUGET Package manager to install the Microsoft.AspNet.WebApi.Cors module.

Then in the WebApiConfig add the following row to the Register function

       // enable CORS support
        config.EnableCors();

Each Controller then needs the following attribute

[EnableCors(origins: "*", headers: "*", methods: "*")]

Note this will allow the API to recieve calls from any client without CORS becoming an issue and should be used with care.

Paul S Chapman
  • 832
  • 10
  • 37