Santhosh
Santhosh

Reputation: 107

Ajax call to web api is Not Working

I'm new to Ajax,I've written my Api in get method it's return value sucessfully i've seen it on web console and also in fiddler but my ajax call to web api is not get any values at document ready function and also not get any error in browser console i don't known why, any one please help me here i attach my api and ajax call.

Api:

[Route("api/GetCompanyName")]
[HttpGet]
public List<Company> GetCompanyName()
{
    var Compname = getcompName();

    return Compname;
}

public List<Company> getcompName()
{
    var CompName = new List<Company>();

    SqlConnection con = new SqlConnection(ConnString);
    con.Open();
    SqlCommand cmd = new SqlCommand("Select * From Company_table", con);
    SqlDataReader dr = cmd.ExecuteReader();
    while (dr.Read())
    {
        CompName.Add(new Company
        {
            companyname = dr["Company_mtName"].ToString(),
            //value = dr["Company_Name"].ToString()
        });
    }
    con.Close();

    return CompName;
}

Ajax call:

<script type="text/javascript">
    $(document).ready(function () {
        $.ajax({
            url:'192.168.1.6:68/api/GetCompanyName',
            type:'GET',
            dataType:'JSON',                                
            crossDomain:true,       

            success: function(data,xhr){
                alert(data);
                }
        });
    });
</script>

Upvotes: 2

Views: 2866

Answers (3)

Ashok
Ashok

Reputation: 56

You need to allow cross domain access in api. For that you need to use System.Web.Http.Cors library.

using System.Web.Http.Cors;

[HttpGet]
[EnableCors("*", "*", "GET")]
public List<Company> GetCompanyName()
{
var Compname = getcompName();
return Compname;
}

In cors first parameter is your host name. " * " means any host can access the method.Second parameter is header and third one is type. " * " can be used to make the method to allow both POST and GET.

Upvotes: 3

Ashok
Ashok

Reputation: 56

try the following code.

$(document).ready(function () {
    $.ajax({
        url:"192.168.1.6:68/api/GetCompanyName",
        type:"GET",
        dataType:"application/json",                                
        crossDomain:true,       

        success: function(data,xhr){
            alert(data);
            }
    });
});

also try with looping the data. Since it is a class object, JSON may not properly show you. Use $.each and loop the data

Upvotes: 1

JBoothUA
JBoothUA

Reputation: 3157

Everything looks ok to me, have you tried adding an error callback as the final param and see if anything happens there?

you can also add a debugger; statement into your code, that will allow you to step through your code and make sure everything is acting accordingly.. debugger style.. (just make sure you have your browsers dev tools open (f12))..

that can help make sure that everything is getting called.

also can you hit that url with a browser and get back data?

Upvotes: 1

Related Questions