Ajax发布不适用于Firefox,但适用于chrome

时间:2019-04-11 12:33:43

标签: javascript ajax reactjs asp.net-web-api

我是ReactJ的新手,我试图从webAPI AJAX帖子中获取一些数据

我的代码在Chrome中工作正常,但在Mozilla Firefox中却无法正常工作 如果我删除$ .ajax api中的标头部分,则调用其工作但标头不起作用。请帮助我我所缺少的内容,或者它如何在Firefox或chrome中运行。

在我的userTypeAPI.js类中

export const constgetUsertype = (authenticationKey) => {
    return getUsertype(authenticationKey);
};
$.support.cors = true;
function getUsertype(authenticationKey) {
    var returnvalue = '';
    $.ajax({
        url: usertypeURL + "/getUserType",
        type: "POST",
        async: false,
        beforeSend: function (xhr) {
            xhr.withCredentials = false;
        },
        xhrFields: {
            withCredentials: false
        },
        crossDomain: true,
        headers: {
            "AuthenticationKey": authenticationKey
        },
        //contentType: 'application/json',
        contentType: "application/x-www-form-urlencoded",
        dataType: "json",
        success: function (data) {
            returnvalue = data.d;
        },
        error: function (request, error) {
            debugger;
            alert(error);
            returnvalue = "";
        }
    });
    return returnvalue;
}

在react.jsx类中

import * as userTypeAPI from '../UserTypeAPI';


    componentDidMount() {
        var usertypes = userTypeAPI.constgetUsertype(sessionStorage.getItem('AuthenticationKey'));

        this.setState({
            options: usertypes
        });
}

在webAPI控制器中,类代码为

 [RoutePrefix("api/UserType")]
    [EnableCors(origins: "*", headers: "*", methods: "*")]
    public class UserTypeController : ControllerBase
    {
        TaskUserType taskUserType = new TaskUserType();

        [HttpPost]
        [ActionName("getUserType")]
        public object getUserType() //Parameter is EntityUserType because we pass UserType information for get list of task
        {
            try
            {
                var requestId = string.Empty;
                var AuthenticationKey = string.Empty;
                var re = Request;
                var headers = re.Headers;
                if (headers.Contains("AuthenticationKey"))
                {
                    AuthenticationKey = headers.GetValues("AuthenticationKey").First();
                }

                long userId = 0;

                if (!Authenticated(AuthenticationKey))
                    return Ok("Unauthorize user");
                else
                    userId = Convert.ToInt64(dtSession.FirstOrDefault().User_Id);

                Dictionary<string, object> returnDict = new Dictionary<string, object>();
                List<EntityUserType> lstUserTypes = new List<EntityUserType>();
                lstUserTypes = taskUserType.getAllUserTypes();
                returnDict = new Dictionary<string, object>();
                returnDict.Add("d", lstUserTypes);
                return returnDict;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
   }

并且webAPI的配置是

    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Headers" value="Overwrite, Destination, Content-Type, Depth, User-Agent, Translate, Range, Content-Range, Timeout, X-File-Size, X-Requested-With, If-Modified-Since, X-File-Name, Cache-Control, Location, Lock-Token, If" />
        <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
        <add name="Access-Control-Allow-Credentials" value="true" />
      </customHeaders>
    </httpProtocol>

我已经检查了 $.ajax post working in Chrome, but not in Firefox链接,但此处显示event.preventDefault();而且在我的代码中没有任何事件。

0 个答案:

没有答案