2

I'm going straight to the point here.

I'm trying to get the value pass from my ajax to controller and console.log the value. however, when I try to console.log the value it gives me error 500..

here's my code:

I've been doing ajax on php for a long time.. however, I'm still new to asp.net C# mvc so please bear with me.

AJAX:

$("#Property_ProvinceID").on("change", function () {
    var $this = $(this);
    var province_id = $this.val();
    var $url = "/Property/GetCities";
    alert("get:" + province_id);
    $.ajax({
        url: $url,
        type: 'POST',
        dataType: 'json',
        contentType: 'application/json; charset=utf-8',
        data:{id: province_id},
        success: function (data) {
            console.log(data);
        }
    });

});

CONTROLLER:

[HttpPost]
public ActionResult GetCities(int id)
{
    return Json(new { success = true });
}

here's the error I don't know what's wrong with my controller though.

POST http://localhost:43969/Property/GetCities 500 (Internal Server Error)

4
  • Does it really hit your GetCities action? Put a break point to check. Also check if you have any ActionFilterAttribute, the error may occur in there. Commented Jan 11, 2017 at 2:57
  • @KimHoang I really am positive that the method GetCities is existing inside my PropertyController class.... I don't know why it won't work though.. the code seems good to me.. Commented Jan 11, 2017 at 2:59
  • One more guess could be the value of your province_id, it may be null or not an invalid integer. I just can suggest that based on information in your question. Commented Jan 11, 2017 at 3:01
  • @KimHoang I alert it before I pass it to the controller and the id is good. Commented Jan 11, 2017 at 3:03

2 Answers 2

2

if using contentType: 'application/json; charset=utf-8' then use JSON.stringify to convert the data being sent to a JSON string.

$("#Property_ProvinceID").on("change", function () {
    var $this = $(this);
    var province_id = $this.val();
    var $url = "/Property/GetCities";
    alert("get:" + province_id);
    var data  = JSON.stringify({id: province_id});
    $.ajax({
        url: $url,
        type: 'POST',
        dataType: 'json',
        contentType: 'application/json; charset=utf-8',
        data: data,
        success: function (data) {
            console.log(data);
        }
    });

});

As mentioned in the comments by @StephenMuecke

It does not need to be stringified if contentType: 'application/json; charset=utf-8', is removed (so that it uses the default application/x-www-form-urlencoded; charset=UTF-8').

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

5 Comments

thanks for this.. so I need to stringify it first... I though {id : province} will suffice.
{id : province} on its own is a javascript value. You need to convert it to JSON.
It does not need to be stringified if contentType: 'application/json; charset=utf-8', is removed (so that it uses the default application/x-www-form-urlencoded; charset=UTF-8'). And [FromBody] is for web-api which this question does not appear to be about?
@StephenMuecke thanks for the clarification. will update.
0

you can add error function to check for possible error on your ajax.

Ex.

error: function(xhr, status, error) {
   var err = eval("(" + xhr.responseText + ")");
   alert(err.Message);
}

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.