0

将 html 数据类型发布到 webapi post 方法时遇到一些问题。

这是邮政编码

    $('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')
      }
    });
});

这是 WebAPI 方法

   public string Post(LeadModel lead)
    {
        try
        {
            svc.AddLead(lead);
        }
        catch
        {
            return "exception";
        }

        return "true";

这就是模型

公共类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; }
}

这是来自 IIS {"Message":"An error has occurred.","ExceptionMessage":"No MediaTypeFormatter is available to read an object of type 'LeadModel' from content with media type'multipart/form-data '.","ExceptionType":"System.InvalidOperationException","StackTrace":" at System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent 内容,类型类型,IEnumerable 1 formatters, IFormatterLogger formatterLogger)\r\n at System.Net.Http.HttpContentExtensions.ReadAsAsync(HttpContent content, Type type, IEnumerable1 格式化程序,IFormatterLogger formatterLogger)\r\n在 System.Web.Http.ModelBinding.FormatterParameterBinding.ExecuteBindingAsync(ModelMetadataProvider metadataProvider, HttpActionContext actionContext, CancellationToken cancelToken)\r\n 在 System.Web.Http.Controllers.HttpActionBinding.<>c_ DisplayClass1.b_0(HttpParameterBinding parameterBinder)\r\n at System.Linq.Enumerable.WhereSelectArrayIterator 2.MoveNext()\r\n at System.Threading.Tasks.TaskHelpers.IterateImpl(IEnumerator1 枚举器,CancellationToken cancelToken)"}

任何线索如何解决这个问题?我们不想发布 jquery 帖子,因为我们必须允许发布跨域。

4

1 回答 1

1

POST方法需要一个LeadModel对象,但您发送的对象中包含 2 个字段(“ action 和“ input ”),因此,预期的类应该是:

public class MyModel
{
    public string action {get;set;}
    public LoadModel input {get;set;}
}

还有你的 webapi 方法:

public string Post(MyModel data)
{
.......
// The input fields should be in "data.input"
.......
}

或者,您可以更改 AJAX 调用,使其仅传递输入字段。喜欢:

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()
}

仅供参考,你说:

我们不想发布 jquery 帖子,因为我们必须允许发布跨域。

但是您正在执行的 AJAX 调用使用的是 POST($.post 与 $.ajax 相同,类型为 POST)

希望能帮助到你。

于 2013-05-21T15:20:12.867 回答