Home > Net >  .net core WebApi receive less than application/json; The value of charset=utf-8
.net core WebApi receive less than application/json; The value of charset=utf-8

Time:11-13

When the parameter marker is [FromBody] receive less than application/json; Charset=utf-8 value can only receive the content-type=the value of the application/json,
How to deal with to receive application/json; Charset=utf-8 value??

CodePudding user response:

What is known as the "application/json; Charset=utf-8 value ", this is just setting data encoding format, in json, or in the XML, charset said character set format,

CodePudding user response:

Parameter is set to a string type, json format can be said,

CodePudding user response:

1. The web.config need to configure the cross-domain
 





2. The front-end code
 
$(" # save "). Click (function () {
JQuery. Support. Cors=true;
Var jdata={https://bbs.csdn.net/topics/"access_token" : "1231", "createtime" : "123", "expires_in" : "123"};
$. Ajax ({
Url: 'http://192.168.1.81:8492/api/values/testPost',
Type: "POST",
ContentType: "application/json; Charset=urf - 8 ",//the specified type is a json string, not a json object
DataType: 'json,
Data: JSON. Stringify (jdata),
CrossDomain: true,
Success: the function (returndata) {
Alert (JSON. Stringify (returndata));
},
Error: function (returndata) {
Alert (2);
}
});
});

3. The backend API
 
[HttpPost, HttpOptions]
[ActionName (" testPost ")]
Public ApiResult testPost ()
{
Try
{
//receive
The Stream inputstream=HttpContext. Current.. Request inputstream;
Byte [] b=new byte [inputstream. Length].
Inputstream. Read (b, 0, (int) inputstream. Length);
String inputstr=UTF8Encoding. And UTF8. Get string (b);

Return new ApiResult
{
IsError=false,
Msg="success",
PageCount=1,
Result=inputstr
};
}
Catch Exception (err)
{
Return new ApiResult
{
IsError=true,
Msg="failure",
PageCount=1,
Result="testPost"
};
}
}

4. Allow the results


CodePudding user response:

If the junction points, please
  • Related