0

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.

3
  • Your contentType should be application/json and also use dataType: "json" Commented Mar 18, 2014 at 7:46
  • Thanks for your reply. I tried your solution also already. My url is server side url and working in local host. Is this gives any issues. Commented Mar 18, 2014 at 8:26
  • What does a tool like Fiddler2 (www.telerik.com/fiddler) say is going on with the requests? Is the server responding at all? You can view everything returned from the cloud service (which may include any errors) in Fiddler. Commented Mar 18, 2014 at 9:18

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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') {

    }
}

Comments

1

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");
                }
            });

3 Comments

Hi Nps, yes i tried that way also. Then only i tried this way. Can any one explain cross domain will affect ajax call. I am call API of Cloud service. and testing this in local host server.
Does it return JSON response? I faced similar type of issue recently in MVC site. I was returning JsonResult with Json(data) method. But it wasn't calling the API but was not able to sent respose in JSON format. In that case neither Success nor error was getting called. When I changed the code and replaced Json(data, JsonRequestBehavior.AllowGet). It started working.
In my case, always failure have been called.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.