AngularJS + ASP.NET Web API Cross-Domain Issue

前端 未结 4 1854
我在风中等你
我在风中等你 2020-12-13 21:49

Moving forward with my mobile app development learning process, I\'ve found a new obstacle: Cross-origin Request Sharing or CORS.

I am using a combination of Angular

4条回答
  •  萌比男神i
    2020-12-13 22:29

    You can skip the preflight option request by using content-type : application/x-www-form-urlencoded.

    AngularJS:

    var user = {
       ID: 1,
       Name: 'test'
    };
    
    $http({
      url: "api.localhost/api/users/addUser",
      method: "POST",
      data: $.param(user),
      headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
      withCredentials: true,
      }).success(function (data, status, headers, config) {
         console.log(data);
    })
    

    Web api:

    [HttpPost]
    public string AddUser(User user)
    {
        // Do something
        return user.Name + " added";
    }
    

    Web.config

        
            
                
                    
                    
                    
                    
                    
    
                    
                    
                    
                
                
                    
                        
                        
                        
                        
                    
                
          
    

提交回复
热议问题