使用axios或fetch呼叫邮政服务

时间:2018-08-09 15:25:53

标签: reactjs rest api asp.net-web-api .net-core

我有一个dotnet核心,它可以在React应用程序中提供此服务:

[HttpPost("[action]")]
        public string  UpdateUserLangage(string liste)
        {
            if(liste != null )
            {
                return liste;
            }
            else
            {
                throw new ArgumentNullException();

            }
        }

我尝试使用axios和访存库调用此服务:

axios

axios.default.post('api/preference/UpdateUserLangage', "test"  )
            .then(function (response) {
                console.log(response);
            })
            .catch(function (error) {
                console.log(error);
            });

获取

fetch('api/preference/UpdateUserLangage', {
            method: 'post',
                body: JSON.stringify("test")
                }).then(function (response) {
                    console.log(response);
                })
            .catch(function (error) {
                console.log(error);
            });

结果,我得到一个错误:

error

参数liste始终为null,因此出现错误!

如何解决此问题?

谢谢

1 个答案:

答案 0 :(得分:0)

此问题与axios或javascript根本无关。您的请求返回500错误。我的猜测是liste为空,并且抛出ArgumentNullException