I cannot capture the post by webapi c#

Asked

Viewed 156 times

0

good morning. I’ve been trying for a few days trying to capture a POST made in Javascript by webapi c# and the most I can is the webapi telling me that the expected parameter is NULL.

var objFornecedor = {
                ID_FORNECEDOR: 38,
                NOME: "cleverton teste",
                CNPJ: "546546546546",
                ENDERECO: "rua c",
                BAIRRO: "novo teste",
                CIDADE: "serrinha",
                SITUACAO: "1",
                DATA_CADASTRO: new Date(2015,02,02)
    }


    $.ajax({
                  contentType: 'application/json; charset=utf-8',
    data: objFornecedor,
          url: 'http://localhost:3190/servicowebapi/fornecedor/incluir',
      success: function(retorno)
        {
              alert('funcionou');..........

and here are the codes you receive

  [HttpPost]
  [ActionName("incluir")]
  public void Post(FORNECEDOR objFornecedor)
  {
      ctx.FORNECEDOR.Add(objFornecedor);
      ctx.SaveChanges();
  }

Call me that mistake. An Exception of type 'System.Argumentnullexception' occurred in Entityframework.dll but was not handled in user code

Additional information: Value cannot be null. ctx.FORNECEDOR.Add(objFornecedor); objFornecedor is NULL

and here follows my class SUPPLIER

public partial class FORNECEDOR
{
    public int ID_FORNECEDOR { get; set; }
    public string NOME { get; set; }
    public string CNPJ { get; set; }
    public string ENDERECO { get; set; }
    public string BAIRRO { get; set; }
    public string CIDADE { get; set; }
    public string SITUACAO { get; set; }
    public Nullable<System.DateTime> DATA_CADASTRO { get; set; }
}

2 answers

2

Use the JSON.stringify:

$.ajax({
    type:'POST',
    contentType: 'application/json; charset=utf-8',
    data: JSON.stringify(objFornecedor),
...
  • I’ve already done whatever you imagine, just tested for a page ASP.Net with the same code to consume and guess ? Asp.net consumes (jQuery), but inside the Cordova plugin I can only read.

  • I’ve already added all the verbs in Webconfig POST, GET, PUT, DELETE, OPTIONS

  • <add name="Access-Control-Allow-Origin" value="*" />

  • 1

    @Cleverton, it would be nice to put in the question that is using Cordova. I did using an ASP.Net page and gave in the same as you. If I find time, I try to do with Cordova.

0

Try it this way:

var objFornecedor = {
        ID_FORNECEDOR: 38,
        NOME: "cleverton teste",
        CNPJ: "546546546546",
        ENDERECO: "rua c",
        BAIRRO: "novo teste",
        CIDADE: "serrinha",
        SITUACAO: "1",
        DATA_CADASTRO: new Date(2015, 02, 02)
    };

    $.ajax({
        dataType: "json",
        contentType: "application/json;charset=utf-8",
        data: JSON.stringify(objFornecedor),
        type: 'POST',
        url: 'http://localhost:3190/servicowebapi/fornecedor/incluir',
        success: function (retorno) {
            alert('funcionou');
        }
    });
  • Xmlhttprequest cannot load http://localhost:3190/servicowebapi/vendor/include? ID_FORNECEDOR=38&NOM... ATA_CADASTRO=Mon+Mar+02+2015+00%3A00%3A00+GMT-0300+(Time+official+do+Brazil). Response for preflight has invalid HTTP status code 405

  • @Cleverton edited the answer, try that code.

  • so, the temporary solution was made through a POG, I made a GET request, I went through the URL. I don’t understand pq using Httppost doesn’t work.

  • I made an example with the same code above and it worked.

  • DO NOT KNOW what happens if I put the verb Httppost in the webapi does not work

Browser other questions tagged

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