2014-04-29 66 views
0

[解决]REST PUT似乎并没有连接到服务器

Subir Kumar Sao完全正确的,这是一个CORS问题,而不是客户端,以防他人通过托管服务器获取同样的问题用C#编码,这将是解决办法:

CORS support for ASP.NET Web API获取组件,并将其引用到你的服务器项目,并在你的HttpSelfHostConfiguration插入这将最终看起来像这样的情况如下:

var config = new HttpSelfHostConfiguration ("http://10.0.5.106:8000"); 

var enableCorsAttribute = new EnableCorsAttribute ("*", "*", "*") 
{ 
    SupportsCredentials = true 
}; 
config.EnableCors (enableCorsAttribute); 

[问题]

我跑我C#下写了VS2013在使用KineticJS画布举办了LED补光灯控制应用程序的服务器。

我测试了它使用邮差扩展和它的工作完美,我能得到,我也可以把。

因为这是我第一次使用REST编码的东西我决定去.ajaxjQuery这似乎很好的文件。但由于一些奇怪的原因,它不会工作,我很遗憾,因为也许我错过了一些东西,而且我也不能检查发生了什么,因为从“错误”返回的前一部分是空的。

这里是我使用的客户端代码:

$.ajax({ 
    url: 'http://10.0.5.106:8000/api/LED/Save', 
    type: 'PUT', 
    data: { N: '1', Pos: '10' }, 
    success: function() { alert('PUT completed'); }, 
    error: function(req,status,ex){alert(ex);} 
}); 

理解我的服务器的结构,那么这里就是藏汉代码:

private static void RestServer() 
{ 
    var config = new HttpSelfHostConfiguration ("http://10.0.5.106:8000"); 

    config.MapHttpAttributeRoutes(); 
    HttpSelfHostServer server = new HttpSelfHostServer (config); 
    server.OpenAsync().Wait(); 
    while (true) { 
    } 
} 

[RoutePrefix ("api/LED")] 
public class LEDController : ApiController 
{ 
    [Route ("Save")] 
    public HttpResponseMessage PutSave(int N, int Pos) 
    { 
     return Request.CreateResponse (HttpStatusCode.NoContent); 
    } 

    [Route ("Load")] 
    public HttpResponseMessage PutLoad(int N, int Pos) 
    { 
     return Request.CreateResponse (HttpStatusCode.NoContent); 
    } 
    [Route ("Intense")] 
    public HttpResponseMessage PutInt(int N, int I) 
    { 
     return Request.CreateResponse (HttpStatusCode.NoContent); 
    } 
    [Route ("RGB")] 
    public HttpResponseMessage PutRGB(int N, int R, int G, int B, int I) 
    { 
     return Request.CreateResponse (HttpStatusCode.NoContent); 
    } 
} 
+1

似乎是一个CORS问题。调用ajax服务的页面是否驻留在同一个域“10.0.5.106”中? –

+0

是的,一切都在同一个域中,但客户端应用程序驻留在不同的PORT中,如果这很重要的话。 –

+0

尝试传递像这样的参数,'data:“N = 1&Pos = 10”,' –

回答

1

这似乎是一个CORS问题。

了解更多here

并阅读here关于如何启用CORS。

相关问题