Return BD values via ajax

Asked

Viewed 184 times

1

I have a screen where I must popular with database values using ajax.

When I click on the button to fill the fields the following error appears:

Failed to load Resource: the server responded with a status of 500 (Internal Server Error)

I don’t know what’s going on.

Model Code:

public class Client
{
    [Key]
    [MaxLength(128)]
    public string client_id { get; set; }

    [MaxLength(20)]
    public string client_cnpj { get; set; }

    [MaxLength(255)]
    public string client_companyname { get; set; }

    [Required]
    [MaxLength(255)]
    public string client_fantasyname { get; set; }

    [Required]
    [MaxLength(128)]
    public string client_namerepresentative { get; set; }

    [Required]
    [MaxLength(15)]
    public string client_cep { get; set; }

    [Required]
    [MaxLength(255)]
    public string client_street { get; set; }

    [Required]
    [MaxLength(10)]
    public string client_number { get; set; }

    [MaxLength(55)]
    public string client_complement { get; set; }

    [Required]
    [MaxLength(128)]
    public string client_neighborhood { get; set; }

    [Required]
    [MaxLength(128)]
    public string client_city { get; set; }

    [Required]
    [MaxLength(2)]
    public string client_uf { get; set; }

    [MaxLength(128)]
    public string client_email { get; set; }

    [MaxLength(15)]
    public string client_phone1 { get; set; }

    [MaxLength(15)]
    public string client_phone2 { get; set; }

    public int? client_currentcarrier { get; set; }

    public int? client_voice { get; set; }

    public int? client_web { get; set; }

    public int? client_renegotiation { get; set; }

    public DateTime? client_datevisitorcontact { get; set; }

    [Required]
    public string client_note { get; set; }

    public int? client_statusnegotiation { get; set; }

    [Required]
    [MaxLength(128)]
    public string client_company_id { get; set; }

    [MaxLength(128)]
    public string client_supervisingconsultant { get; set; }

    [MaxLength(128)]
    public string client_supervisingsalessupport { get; set; }

    [MaxLength(128)]
    public string client_consultant { get; set; }

    [MaxLength(128)]
    public string client_salessupport { get; set; }

    [MaxLength(128)]
    public string client_source { get; set; }

    public bool client_return { get; set; }

    public bool client_sendemail { get; set; }

    public DateTime client_dateregister { get; set; }

    [ForeignKey("client_company_id")]
    public virtual Company Company { get; set; }

    public virtual ICollection<HistoricFunnel> HistoricFunnel { get; set; }

    public virtual ICollection<ActivityClient> ActivityClient { get; set; }
}

Controller Code:

[HttpPost]
public JsonResult ReturnVisit(string id)
{
    var visit = _clientAppService.GetById(id);
    return Json(new { visit = visit });
}

Javascript code:

$(".visits-table tr a").click(function () {
    var id = $(this).parent().parent().attr("id");

    $.ajax({
        dataType: "json",
        type: "POST",
        url: "/Visits/ReturnVisit",
        data: { id: id },
        success: function (data) {
            alert("entrou")
            alert(data)
        }
    });

    return false;
});
  • a comma is missing after date: { id: id },

  • In the code has the comma rs, I deleted a comment that had below, then the comma went together. It goes to the server, only does not return to class.

  • tries to do the same example below by removing [Httppost]

3 answers

1

Error 500 is a generic HTTP error, you don’t know what’s going on. You need to read the page with the result of the request. Also, if your IIS is not configured to show errors it will not solve. Try setting up IIS to turn off "customErrors" by configuring it on Web.onfig

<system.web>
    <customErrors mode="Off"></customErrors>
</system.web>

Then run your error code and check exactly what error is occurring. If you’re using it to debug Firefox, use Firebug and see the Request Ajax result in the Console tab. Chrome is the same thing. To enter this console press F12.

Another thing, you seem to be trying to return a "visit" domain class. You should never expose a domain class, always turn your domain class into a Viewmodel class (POCO). This avoids cyclic reference and other problems when exposing a domain class.

  • 1

    I will make the modifications. I will use Mapper to solve the domain problem.

  • This error appeared on return: Unable to find resource. Description: HTTP 404. The resource you are looking for (or one of its dependencies) could not be removed, its name has been changed or is temporarily unavailable. Scan the URL and make sure it is typed correctly. URL requested: /Visits/Returnvisit

  • HTTP 404 means page not found. Somehow the request did not find its method. If you have changed your ajax to make a GET and the method is still with the Httppost attribute it will not find your me all /Visits/Returnvisit

  • It is like post in ajax tbm, so I don’t understand.

  • Take a look at the console for the URL that is being requested. Watch carefully if there are no typos. HTTP 40 is Not Found. http://www.w3schools.com/tags/ref_httpmessages.asp

  • But when I modify the controller return type it works normally.

  • When I put this return works: var result = new { client = "Line of Code", URL = "www.linhadecodigo.com.br" }; Return Json(result, Jsonrequestbehavior.Denyget);

  • Just looking at the code. HTTP 404 is Not found. Http 500 is a generic error. If you send a POST, your method waits for an ID parameter and you do not send an ID, I believe it returns 404 as well. A good practice is to define a class in the method that receives the post instead of setting a primitive type as a parameter. Try something like public class Returnvisit(Visitpost post) { var c = _db.Getbyid(post.Id); Return Json(c); } and create the class: public class Visitpost { public int Id { get;set;} }

  • I’ll test it here.

  • Anything put its code here: http://collabedit.com/xyyht

Show 5 more comments

0

Try to do it by GET

$(".visits-table tr a").click(function () {
var id = $(this).parent().parent().attr("id");

$.ajax({
    dataType: "json",
    type: "GET",
    url: "/Visits/ReturnVisit",
    data: { id: id },
    success: function (data) {
        alert("entrou");
        alert(data);
    }
});

return false;

});

public JsonResult ReturnVisit(string id)
{
    var visit = _clientAppService.GetAll();
    return Json(visit,JsonRequestBehavior.AllowGet);
}

0

Depending on your application’s IIS post, put the URL directly to controller/action may not be legal. Use the power of razor.

$(".visits-table tr a").click(function () {
    var id = $(this).parent().parent().attr("id");

    $.ajax({
        dataType: "json",
        type: "POST",
        url: '@Url.Action("ReturnVisit", "Visits")',
        data: { id: id }
        success: function (data) {
            alert("entrou")
            alert(data)
        }
    });

    return false;
});

Take care of requests... if the controller method is noted as [HttpGet] you must make a request GET. If you’re like [HttpPost] you must make a request POST.

Your controller

[HttpPost]
public JsonResult ReturnVisit(string id)
{
    var visit = _clientAppService.GetAll();
    return new JsonResult { Data = visit };
}

Your model has to be [Serializable]:

using System;

[Serializable]
public class Client
{

....

}

Every property that is a Class, that class has to be serialized as well.

More about the serialization

  • It this Httppost, the url works perfectly, at the time of returning q the stick.

  • I’ll test with that code.

  • I put an edit on the controller’s return... _clientAppService.GetAll()? Because the id that you receive per parameter is neither used....

  • It returns a single client, was testing with a list, but tbm did not work, the correct is Getbyid(id).

  • What object do you return? You can put his class there in the question?

  • I’ll put it right now.

  • And you need to return all the filled properties? You have to serialize this class then... I’ll edit my answer for you to see.

  • No, just a few. I’ll modify here.

  • It didn’t work Marllon.

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.