user1120998
user1120998

Reputation: 229

Ajax does not run the Web API url on client side in Asp.net

I have written API in Cloud service using web role. This api is working fine (cheked via postman, .net Webclient, etc). But it does not working only via Ajax call in client side.

               var car = { minlat: '11', minlong: '45', maxlat: '-44', maxlong:'130', locationlevel: 'city' };

            $.ajax({
                async: true,
                type: "POST",
                contentType:"application/x-www-form-urlencoded; charset=utf-8",
                url: "url",
                data: JSON.stringify(car), 
               // dataType: "json",
                success: function (data) {
                    var obj = data.d;
                    if (obj == 'true') {

                    }
                },
                error: function (result) {
                    alert("Error");
                }
            });

Can anyone explain that cross domain will affect ajax call? I am calling API of Cloud service. And testing this in local host server.

Upvotes: 0

Views: 392

Answers (3)

Nps
Nps

Reputation: 1658

try passing data without stringify them

var car = { minlat: '11', minlong: '45', maxlat: '-44', maxlong:'130', locationlevel: 'city' };

            $.ajax({
                async: true,
                type: "POST",
                contentType:"application/json; charset=utf-8",
                url: "url",
                data: car, 
                dataType: "json",
                success: function (data) {
                    var obj = data.d;
                    if (obj == 'true') {

                    }
                },
                error: function (result) {
                    alert("Error");
                }
            });

Upvotes: 1

Anastasios Vlasopoulos
Anastasios Vlasopoulos

Reputation: 1802

You should set what kind of data you are expecting as return. As Felix told you, you should use the

datatype: "JSON"

Also if your returned json string is a json array, then the success function should be something like this:

success: function (data) {
    var obj = data[0].d;
    if (obj == 'true') {

    }
}

Upvotes: 1

Felix
Felix

Reputation: 38112

You can add dataType: json to your AJAX call since you're working with JSON.

Upvotes: 1

Related Questions