1

I have a Web API 2.0 action that does the following:

  [System.Web.Http.Route("api/wdretrievedata")]
    [System.Web.Http.HttpGet]
    public IHttpActionResult RetrieveUserData(string email)
    {
        var user = new WDUserData();
        user.FirstName = "FirstName";
        user.LastName="LastName";
        user.PhoneNumber="PhoneNumber";
        return Ok(user);

    }

AS you can see, no errors are here. I run this in Postman and get results (omitted certain data for security):

enter image description here

I run this in my code via $.ajax like so:

 $.ajax({
                url: 'http://localhost:49352/api/wdretrievedata?email=myemail@gmail.com',
                type: "GET",
                async: true,

                success: function (data) {
                   //never goes here
                },
                error: function (err) {
                     //always goes here
                    //err.statusText says 'error' and nothing else
                        alert(err.statusText);
                }
            });

The API call always returns in the error section and contains no data whatsoever. How do I get my API call to work via the $.ajax call like Postman? Thanks

If this helps, i've deployed my api to my web server. Troy this url to see what i'm getting:

http://tce-windows-env.hiujwmcthx.us-east-2.elasticbeanstalk.com/api/wdretrievedata?email=troy.compton@gmail.com

BoundForGlory
  • 4,114
  • 15
  • 54
  • 81

2 Answers2

1

After some research with @BoundForGlory, it was detected that problem with Cross-Origin Requests.

One of options is to add custom header Access-Control-Allow-Origin to allow CORS

<httpProtocol>
    <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
    </customHeaders>
</httpProtocol>
demo
  • 6,038
  • 19
  • 75
  • 149
-1

Try:

[System.Web.Http.Route("api/wdretrievedata")]
[System.Web.Http.HttpGet]
public async Task<IHttpActionResult> RetrieveUserData(string email)
{
    var user =  await new WDUserData();
    user.FirstName = "FirstName";
    user.LastName="LastName";
    user.PhoneNumber="PhoneNumber";
    return Ok(user);

}
Jester
  • 3,069
  • 5
  • 30
  • 44