Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
asp.net webapi POST可以在Postman上工作,但不能在浏览器AJAX请求中工作_Asp.net_Ajax_Asp.net Web Api - Fatal编程技术网

asp.net webapi POST可以在Postman上工作,但不能在浏览器AJAX请求中工作

asp.net webapi POST可以在Postman上工作,但不能在浏览器AJAX请求中工作,asp.net,ajax,asp.net-web-api,Asp.net,Ajax,Asp.net Web Api,我的asp.net HttpPost测试应用程序在Postman中运行良好,但在HTML AJAX请求中返回错误 我的控制器: public class ContactController : ApiController { [HttpPost] public string Post([FromBody] myData m) { return String.Format("Test A"); } } 类别: public class myData

我的asp.net HttpPost测试应用程序在Postman中运行良好,但在HTML AJAX请求中返回错误

我的控制器:

public class ContactController : ApiController
{
    [HttpPost]
    public string Post([FromBody] myData m)
    {
        return String.Format("Test A");
    }
}
类别:

public class myData
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }
}
如果我在Postman中使用URL:和正文运行请求:

{ 
    "FirstName" : "FName", 
    "LastName" : "LName" 
}
它跑得很好!我看到输出:“testa”

但是,当我尝试使用HTML Ajax请求时:

    <html>

<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</head>

<body>
    <script>
        $.ajax(
        {
            url: "http://localhost:52884/api/contact",
            type: "POST",
            dataType: 'jsonp',
            data: { FirstName: "FName", LastName: "LName" },
            success: function (result) {
                alert(result);
            },
            error: function (xhr, status, p3, p4) {
                console.debug(xhr);
                var err = "Error " + " " + status + " " + p3;
                if (xhr.responseText && xhr.responseText[0] == "{")
                    err = JSON.parse(xhr.responseText).message;
                alert(err);
            }
        });
    </script>
</body>

</html>

$.ajax(
{
url:“http://localhost:52884/api/contact",
类型:“POST”,
数据类型:“jsonp”,
数据:{FirstName:“FName”,LastName:“LName”},
成功:功能(结果){
警报(结果);
},
错误:功能(xhr、状态、p3、p4){
调试(xhr);
var err=“Error”+“”+状态+“”+p3;
if(xhr.responseText&&xhr.responseText[0]==“{”)
err=JSON.parse(xhr.responseText).message;
警惕(err);
}
});
我在控制台中看到错误:

The character encoding of the HTML document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. The character encoding of the page must be declared in the document or in the transfer protocol
Loading failed for the <script> with source “http://localhost:52884/api/contact?callback=jQuery112409902197956907268_1507917530625&FirstName=FName&LastName=LName&_=1507917530626”.
未声明HTML文档的字符编码。如果文档包含US-ASCII范围之外的字符,则在某些浏览器配置中,文档将以乱码文本呈现。页面的字符编码必须在文档或传输协议中声明
加载源为“”的文件失败http://localhost:52884/api/contact?callback=jQuery112409902197956907268_1507917530625&FirstName=FName&LastName=LName&_=1507917530626”.
晚安

您需要在对象中针对{FirstName:“FName”,LastName:“LName”}使用JSON.stringfy

见示例:

常量数据={FirstName:“FName”,LastName:“LName”} const jsonData=JSON.stringfy(数据)

url:“”, 类型:“POST”, 数据类型:“jsonp”, 数据:jsonData,/{FirstName:“FName”,LastName:“LName”}, 成功:功能(结果){ 警报(结果); },

希望我能帮助你。

晚安

您需要在对象中针对{FirstName:“FName”,LastName:“LName”}使用JSON.stringfy

见示例:

常量数据={FirstName:“FName”,LastName:“LName”} const jsonData=JSON.stringfy(数据)

url:“”, 类型:“POST”, 数据类型:“jsonp”, 数据:jsonData,/{FirstName:“FName”,LastName:“LName”}, 成功:功能(结果){ 警报(结果); },


我希望我能帮助您。

请将
数据类型:“jsonp”
更改为
数据类型:“json”
。您正在使用jsonp,但代码中没有回调。您是否尝试过使用
json.stringyfy({FirstName:“FName”,LastName:“LName”})
您使用的是哪个版本的web api?您是否在配置文件中设置了json格式的默认返回值?否,它不起作用。请尝试从Visual Studio项目外部的文件调用index.html。如果我删除了jsonp,我收到了CORS错误,请将
数据类型:'jsonp'
更改为
数据类型:“json”
。您正在使用jsonp,但代码中没有回调。您是否尝试过使用
JSON.stringyfy({FirstName:“FName”,LastName:“LName”})
您使用的是哪个版本的web api?是否在配置文件中设置了json格式的默认返回值?否,它不起作用。请尝试从Visual Studio项目外部的文件调用index.html。如果删除jsonp,则会出现CORS错误