javascript - Posting datatype html to ASP.NET WebAPI -


have problem post html datatype webapi post method.

this post code

    $('form#katalog').submit(function(e) {     e.preventdefault();     var data = {         action: 'katalog-callback',         input: {             firstname: $('form#katalog #firstname').val(),             lastname: $('form#katalog #lastname').val(),             address: $('form#katalog #address').val(),             zip:$('form#katalog #zip').val(),             city: $('form#katalog #city').val(),             phone: $('form#katalog #tel').val(),             mobile: $('form#katalog #mobile').val(),             email: $('form#katalog #email').val(),             buildstart: $('form#katalog #build-start').val(),             plot: $('form#katalog #plot').val(),             buildcity: $('form#katalog #build-city').val()         }     };      var request = $.ajax({       url: "/ajax",       type: "post",       data: data,       datatype: "html"     });      request.done(function(result) {       if (result == 1) {           alert('thx!')       } else {           alert('error')       }     }); }); 

and webapi method

   public string post(leadmodel lead)     {         try         {             svc.addlead(lead);         }         catch         {             return "exception";         }          return "true"; 

and model

public class leadmodel

{     public string firstname { get; set; }     public string lastname { get; set; }     public string address { get; set; }     public string zip { get; set; }     public string city { get; set; }     public string phone { get; set; }     public string mobile { get; set; }     public string email { get; set; }     public string buildstart { get; set; }     public string plot { get; set; }     public string buildcity { get; set; } } 

and error message iis {"message":"an error has occurred.","exceptionmessage":"no mediatypeformatter available read object of type 'leadmodel' content media type 'multipart/form-data'.","exceptiontype":"system.invalidoperationexception","stacktrace":" @ system.net.http.httpcontentextensions.readasasync[t](httpcontent content, type type, ienumerable1 formatters, iformatterlogger formatterlogger)\r\n @ system.net.http.httpcontentextensions.readasasync(httpcontent content, type type, ienumerable1 formatters, iformatterlogger formatterlogger)\r\n @ system.web.http.modelbinding.formatterparameterbinding.executebindingasync(modelmetadataprovider metadataprovider, httpactioncontext actioncontext, cancellationtoken cancellationtoken)\r\n @ system.web.http.controllers.httpactionbinding.<>c_displayclass1.b_0(httpparameterbinding parameterbinder)\r\n @ system.linq.enumerable.whereselectarrayiterator2.movenext()\r\n @ system.threading.tasks.taskhelpers.iterateimpl(ienumerator1 enumerator, cancellationtoken cancellationtoken)"}

any clue how solve this? dont want jquery post because have allow post cross domains.

the post method expecting leadmodel object, sending object 2 fields in ("action" , "input"), so, expected class should be:

public class mymodel {     public string action {get;set;}     public loadmodel input {get;set;} } 

and webapi method:

public string post(mymodel data) { ....... // input fields should in "data.input" ....... } 

or, change ajax call passes input fields. like:

var data = {         firstname: $('form#katalog #firstname').val(),         lastname: $('form#katalog #lastname').val(),         address: $('form#katalog #address').val(),         zip:$('form#katalog #zip').val(),         city: $('form#katalog #city').val(),         phone: $('form#katalog #tel').val(),         mobile: $('form#katalog #mobile').val(),         email: $('form#katalog #email').val(),         buildstart: $('form#katalog #build-start').val(),         plot: $('form#katalog #plot').val(),         buildcity: $('form#katalog #build-city').val() } 

fyi, said:

we dont want jquery post because have allow post cross domains.

but ajax call doing using post ($.post same $.ajax post type)

hope helps.


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -