Webservice返回404(找不到) [英] Webservice returns 404 (Not Found)

查看:3153
本文介绍了Webservice返回404(找不到)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在建立一个web服务。该服务是在我的本地主机上构建的,因此当我在浏览器中访问该网址时,例如 http://127.0 .0.1 / api / MyTest / GetStatus 实际上会响应期望的消息。

 < Error> 
< Message>
请求的资源不支持http方法GET。
< / Message>
< / Error>

但是,当我尝试使用脚本文件命中时,它返回404未找到。消息返回为找不到与请求URIhttp://127.0.0.1/api/MyTest/GetStatus匹配的HTTP资源。

 < script src =http://code.jquery.com/jquery-latest.min.jstype =text / javascript> ;< / script> 
< script type =text / javascript>
$ .ajax({
url:'http://127.0.0.1/api/MyTest/GetStatus',
type:'POST',
contenttype:'application / json; charset = utf-16',
success:function(msg){
console.log(msg);
}
}
< / script>

我在其他服务上使用的同一个脚本,他们都正确响应。任何想法?

解决方案

我会做一些假设,看看你没有提供给我们您的ServerSide代码。



我假设您使用的是WebApi,而且您的控制器方法看起来与此类似:

  public class MyTestController:ApiController 
{
[HttpPost,ActionName(GetStatus)]
public void GetStatus(string value)
{

}
}

这将解释为什么你会得到


请求的资源不支持http方法GET。



$ b



您在POST中找不到404的原因可能是因为您没有显示任何数据



尝试更改您的AJAX调用以包含POST数据:

  $。ajax({
url:'http://127.0.0.1/api/MyTest/GetStatus',
type:'POST',
data:{someData}
contenttype:'application / json; charset = utf-16',
success:function(msg){
console.log(msg);
}
});

确保您的POST数据格式与您的Controller方法签名匹配。





您调用了Controller方法GetStatus,因此我假设您尝试从服务器获取一些状态。 / p>

真的需要POST吗?



如果是,您应该将您的方法签名更改为:

  public class MyTestController:ApiController 
{
[HttpGet,ActionName(GetStatus)]
public string GetStatus()
{
returnSomeStatus;
}
}

并将您的AJAX调用更新为:

  $。ajax({
url:'http://127.0.0.1/api/MyTest/GetStatus',
type:'GET',
contenttype:'application / json; charset = utf-16',
success:function(msg){
console.log(msg);
}
});

希望有帮助。


Hi all I am building a webservice. The service has been built on my localhost so when I hit the URL in browser, for example, http://127.0.0.1/api/MyTest/GetStatus would actually respond with expected message.

<Error>
<Message>
The requested resource does not support http method 'GET'.
</Message>
</Error>

However it returns a 404 not found when I try to hit that with a script file. Message returns as "No HTTP resource was found that matches the request URI 'http://127.0.0.1/api/MyTest/GetStatus'."

<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script type="text/javascript">
$.ajax({
    url: 'http://127.0.0.1/api/MyTest/GetStatus',   
    type: 'POST',    
    contenttype: 'application/json; charset=utf-16',   
    success: function (msg) {
        console.log(msg);
    }
});
</script>

The same script I have been using on other services and they are all responding correctly. Any idea?

解决方案

I'm going to make a few assumptions seeing that you didn't provide us with your ServerSide Code.

I'm assuming you are using WebApi and that your controller method looks similar to this:

public class MyTestController : ApiController
{
    [HttpPost, ActionName("GetStatus")]
    public void GetStatus(string value)
    {

    }
 }

This would explain why you are getting

The requested resource does not support http method 'GET'.

when you make the request through the browser.

The reason why you are getting 404 not found on your POST is probably because you are not POSTING any data along with the request but your Controller Method is expected some value.

Either try changing your AJAX call to include POST data :

$.ajax({
    url: 'http://127.0.0.1/api/MyTest/GetStatus',   
    type: 'POST',    
    data: {someData}
    contenttype: 'application/json; charset=utf-16',   
    success: function (msg) {
        console.log(msg);
    }
});

Ensuring that your POST data format matches your Controller method signature.

OR

You called your Controller method "GetStatus", so I'm assuming you're trying to get some status back from your server.

Is there really a need for a POST? Did you perhaps mean to use GET instead?

If so, you should change your Method signature to:

public class MyTestController : ApiController
{
    [HttpGet, ActionName("GetStatus")]
    public string GetStatus()
    {
        return "SomeStatus";
    }
}

And update your AJAX call to:

$.ajax({
    url: 'http://127.0.0.1/api/MyTest/GetStatus',   
    type: 'GET',    
    contenttype: 'application/json; charset=utf-16',   
    success: function (msg) {
        console.log(msg);
    }
});

Hope that helps.

这篇关于Webservice返回404(找不到)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆