3

I have a controle like this

public JsonResult GetSizes(long Id)
    {
        try
        {
            //get some data and filter y Id

        }
        catch (Exception ex) {  }
        return Json(//data);
    }

I need to get that by following json by ajax request

var sizes = [];
$.ajax({
    type: 'POST',
    async: false,
    data: { 'Id': selectedId },
    url: "/<Controler name>/GetSizes",
    dataType: 'json',
    contentType: 'application/json; charset=utf-8',
    error: function (xhr) {
        alert('Error: ' + xhr.statusText);
        return false;
    },
    success: function (result) {
        if (result.Result != null) {
            if (result.Result.length > 0) {
                sizes = result;
            }
        }
    }
});

But this give me an Server error. How can i fix this.

4
  • 1
    what is the Server error? Can you post that? and GetSizes doesn't help to figure out problem Commented Mar 18, 2014 at 7:28
  • 1
    I hope that this line url: "/<Controler name>/GetSizes", has the correct controller name in it and not this <Controler name> placeholder. Otherwise, there's your error source. On a different note, generate the url using "@Url.Action("controller_name", "action_name")" by filling in the correct controller and action names. Commented Mar 18, 2014 at 7:29
  • Controller name is correct. But it give "Error: Internal Server Error" Commented Mar 18, 2014 at 7:46
  • i think problem with data return. return statement with return Json("data", JsonRequestBehavior.AllowGet); add JsonRequestBehavior.AllowGet in json return. Commented Mar 18, 2014 at 8:24

3 Answers 3

2

replace your

url: "/<Controler name>/GetSizes",

by

url: "@Url.Action("GetSizes", "Controller_Name"),

and is you Ajax will have to be

async: false?


then try to use this as your Action

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult GetSizes(long Id)
{
    try
    {
        //get some data and filter y Id

    }
    catch (Exception ex) {  }
    return Json(//data);
}

Also, try to put a break point on your action and see in debug mode if your Ajax reaches your Action.

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

Comments

0

this my demo, you can do the same:

$.ajax({
                        url: '@Url.Action("CheckCity", "BookingStarts")',
                        data: { packageId: packageid, cityId: valuecities[valuecities.length - 1] },
                        type: 'POST',
                        dataType: 'json',
                        success:
                            function(result) {
                                if (result.Status == true) {
                                    $('#[email protected]').val(result.Date);
                                }
                            }
                    });

in controller :

 [AcceptVerbs(HttpVerbs.Post)]

public ActionResult CheckCity(int packageId, int cityId) {

var packageCityModel = PackageDetails.GetPackageCitiesByPackageId(packageId).OfType<HMSService.PackageCity>();
var package = new PackageReservationMasterDal();
var itemPackage = package.GetPackageDetailByPackageId(packageId);
var result = "";
var city = packageCityModel.FirstOrDefault(x => x.CityId == cityId);
if (city != null)
{
    result = itemPackage.TravelDateFrom.AddDays(city.NoOfNights).ToShortDateString();
}
return Json(new { Status = true, Date = result });

}

Comments

0

See the problem here is that you have not stringified your Data Transfer Object(DTO).
A cleaner approach will be this.

<script type="text/javascript" src="https://code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/json3/3.3.0/json3.js"></script>
<script type="text/javascript">
    var sizes = [];
    var DTO = { 'Id': selectedId };
    $.ajax({
        type: 'POST',
        async: false,
        data: JSON.stringify(DTO),
        url: "@Url.Action("GetSizes", "Home")",
        dataType: 'json',
        contentType: 'application/json'
    }).done(function(result) {
        if (result.Result != null) {
            if (result.Result.length > 0) {
                sizes = result;
            }
        }
    }).fail(function(xhr) {
            alert('Error: ' + xhr.statusText);
            return false;
    });
</script>

Please note the use of

  1. JSON.stringify
  2. @Url.Action helper
  3. jqXHR.done(function( data, textStatus, jqXHR ) {});
  4. jqXHR.fail(function( jqXHR, textStatus, errorThrown ) {});

Also you are using async=false which I guess is to grab all the sizes before exiting from the function. jQuery.deferred will be an interesting read.

Comments

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.