zoukankan      html  css  js  c++  java
  • 【WEBAPI】关于WEBAPI无法获取数据的问题之一(Issues while making a POST to a Web API from JQuery)

    I have a web api with the following POST Method

    public HttpResponseMessage Post([FromBody]string package)
    

      


    I have a console app that uses the HttpCLient with no problems. When I try to make a call by means of jQuery, I get null on the package variable.

    This is the code I have right now:

    $.ajax({
    url: 'http://localhost:8081/api/Package/',
    type: 'POST',
    data: JSON.stringify(message), 
    contentType: "application/json;charset=utf-8",
    success: function (data) {
    alert(data.length);
    
    },
    error: function (xhr, ajaxOptions, thrownError) {
    alert('Status: '+xhr.status+', Error Thrown: '+thrownError);
    
    }
    });
    

      

    直接采用上面的代码,无法获取数据,在参数前加个 "=",就可以映射到服务参数中去。如下代码

    $.ajax({
    url: 'http://localhost:8081/api/Package/',
    type: 'POST',
    data: "=" + JSON.stringify(message), 
    contentType: "application/json;charset=utf-8",
    success: function (data) {
    alert(data.length);
    
    },
    error: function (xhr, ajaxOptions, thrownError) {
    alert('Status: '+xhr.status+', Error Thrown: '+thrownError);
    
    }
    });
    

      

  • 相关阅读:
    求给定数里的数的质数最大——pku3048
    poj1106
    poj1450
    poj1094
    poj1111
    poj1120
    C#.NET学习笔记 类,接口,对象
    在Repeater中嵌套使用Repeater
    数据表分区解决方案(转)
    C#小数点格式化
  • 原文地址:https://www.cnblogs.com/taoqianbao/p/2931638.html
Copyright © 2011-2022 走看看