Sending Data to ServiceStack RESTful service, getting 'Access is denied'

后端 未结 1 863
猫巷女王i
猫巷女王i 2020-12-07 00:21

I built a RESTful service with ServiceStack which sends data to a database. I\'ve tested it locally and it works great. When I deploy it to a server and run the same code,

相关标签:
1条回答
  • 2020-12-07 00:24

    I had the same problem with you, in my previous question

    You can read very helpful answers of #mythz here and here .

    The code I use in the AppHost

              using System.Web;
              using ServiceStack.WebHost.Endpoints.Extensions;  // for  httpExtensions methods  
        //  => after  v.3.9.60,  =>using ServiceStack;
    

    and

              public override void Configure(Container container)
              {   
    
                    SetConfig(new ServiceStack.WebHost.Endpoints.EndpointHostConfig
                    {
                     DefaultContentType = ContentType.Json,
                      ReturnsInnerException = true,
                      WsdlServiceNamespace = "http://www.servicestack.net/types"
                    });
    
                 Plugins.Add(new CorsFeature()); 
                 this.RequestFilters.Add((httpReq, httpRes, requestDto) =>
                 {
                    //Handles Request and closes Responses after emitting global HTTP Headers
                       if (httpReq.HttpMethod == "OPTIONS")
                               httpRes.EndServiceStackRequest();  //httpExtensions method
                   //  =>after  v.3.9.60, => httpRes.EndRequestWithNoContent(); 
                  });
    
                Routes
                 .Add<TestRequest>("/TestAPI/Reservation", "POST, OPTIONS");   // OPTIONS is mandatory for CORS
              }
    

    and in JavaScript like you

      jQuery.support.cors = true;
    
        function TestRequestCall() {
           var  TestRequest = new Object();                
               TestRequest.Id = 11111;
               TestRequest.City = "New York";
    
    
    
          $.ajax({
              type: 'Post',
              contentType: 'application/json',         
              url: serverIP +'/TestAPI/Reservation',
              data: JSON.stringify( TestRequest ),
              dataType: "json",
              success: function (TestResponse, status, xhr) {
    
                     if(TestResponse.Accepted)  doSomething();
    
               },
               error: function (xhr, err) {
                  alert(err);
               }
          });
       }
    
    0 讨论(0)
提交回复
热议问题