0

I'm trying to fetch data from a controller and update my view using ajax.

This is my controller:

public class PatientController
{
    DatabaseContext db = new DatabaseContext();
    public JsonResult GetPatientFromCpr()
    {
        var patient = db.Patients.FirstOrDefault(p => p.Cpr == "2410911615");
        return new JsonResult() { Data = patient, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
    } 
}

And this is my ajax call:

    function getPatient() {
    cpr2 = $("#cpr-first").val() + $("#cpr-last").val();
    $.ajax(
    {
        url: '/Patient/GetPatientFromCpr',
        dataType: 'json',
        success: function () {
            alert("success");
        },
        error: function () {
            alert("error");
        },
    });
}

When i call the function i always get the error alert.

GET http://localhost:51140/Patient/GetPatientFromCpr 404 (Not Found)

Can someone point out what's wrong?

(EDIT) I now get a new error after adding ": Controller"

GET http://localhost:51140/Patient/GetPatientFromCpr 500 (Internal Server Error)

8
  • 1
    where is the type , add type:"Get" after url call Commented Feb 5, 2016 at 10:20
  • 3
    The url looks correct. What happens when you enter it in the address bar and navigate to it? Are your using areas? Commented Feb 5, 2016 at 10:20
  • @SrinivasR, The default is 'GET' Commented Feb 5, 2016 at 10:21
  • Can u please share the routes Commented Feb 5, 2016 at 10:23
  • @StephenMuecke I get 404 when entering it. Areas? Commented Feb 5, 2016 at 10:41

3 Answers 3

1

Your 'PatientController' is not a Controller (it does not inherit from Controller)

public class PatientController : Controller
{
   ....
}
Sign up to request clarification or add additional context in comments.

8 Comments

Wow i'm stupid. Thanks, but it didn't fix my error (Well it must have fixed something)
Are you saying your still getting a 404 Not Found?
Ahh no you are right.. GET localhost:51140/Patient/GetPatientFromCpr 500 (Internal Server Error)
Then that means your server code is throwing an exception - you need to debug your code (and you can use your browser tools to inspect the response and the error details (Network Tab). And please undo your edit to the question - or I will - you cant just change the question based on a correct answer (in makes my answer pointless)
@Smit. NO! OP is sending back a JsonResult so it needs to be dataType: 'json' and its a GET method so it needs to be type: 'get' but that is the default so it can be omitted.
|
0

Inherit you PatientController with Base Controller Class

public class PatientController : Controller

In controller

 public JsonResult GetPatientFromCpr()
    {
        var patient = db.Patients.where(p => p.Cpr == "2410911615").FirstOrDefault();
        return new JsonResult() { Data = patient, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
    } 

And Specify the type of ajax calling

type: "POST" or type: "GET" ....

This will help you to fix error

1 Comment

Thanks for your response, but it has already been replied. I now get a new error (500).
0

Try using this, may be this works..!!

View:

function getPatient() {
    cpr2 = $("#cpr-first").val() + $("#cpr-last").val();
    $.ajax(
    {
        url: '/Patient/GetPatientFromCpr',
        //dataType: 'json',
        type:"POST", // GET or POST
        success: function () {
            alert("success");
        },
        error: function () {
            alert("error");
        },
    });
}

Controller:

public class PatientController : Controller
{
   ....
}

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.