jQuery Ajax调用WEB API


                    

我正在尝试对WEB API方法进行简单的jquery ajax调用。</ p>

   &lt;/ script&gt;<br>
 <script type =“ text / javascript”></p>

<p>     $(document).ready(function(){</p>

<p>         $ .ajax({<br>
             输入:“ POST”,<br>
             网址:“ <a href="http://redrock.com:6606/api/values/get%E2%80%9D%EF%BC%8C">http://redrock.com:6606/api/values/get”,</a></p>

<p>             dataType:“ jsonp”,</p>

<p>             crossDomain:是的,<br>
             成功:功能(msg){</p>

<p>                 警报(“成功”);</p>

<p>             },<br>
             错误:功能(请求,状态,错误){</p>

<p>                 警报(错误);<br>
             }<br>
         });<br>
     });</p>

<p>&lt;/ script&gt;<br>
 &lt;/ code&gt; &lt;/ pre&gt;</p>

<p><p> WEB API操作:&lt;/ p&gt;</p>

<p><pre> <code>公共IEnumerable <string> Get()<br>
   {<br>
      返回新的字符串[] {“ value1”,“ value2”};<br>
   }<br>
 &lt;/ code&gt; &lt;/ pre&gt;</p>

<p><p> ajax调用未达到WEB API。 我在浏览器控制台中收到以下错误。&lt;/ p&gt;</p>

<p><p> GET <a href="http://redrock.com:6606/api/values/get?callback=jQuery18207315279033500701_1383300951840&_=1383300951850" rel="noreferrer"> <a href="http://redrock.com:6606/api/values">http://redrock.com:6606/api/values</a>  / get?callback = jQuery18207315279033500701_1383300951840&_ = 1383300951850 </a> 400(错误请求)&lt;/ p&gt;<br>
     &lt;/ div&gt;</p>

展开原文

原文

I'm trying to make a simple jquery ajax call to a WEB API method.

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> 
<script type="text/javascript">

    $(document).ready(function () {

        $.ajax({
            type: 'POST',
            url: 'http://redrock.com:6606/api/values/get',

            dataType: "jsonp",

            crossDomain: true,
            success: function (msg) {

                alert("success");

            },
            error: function (request, status, error) {

                alert(error);
            }
        });
    });

</script>

WEB API action:

 public IEnumerable<string> Get()
  {
     return new string[] { "value1", "value2" };
  }

ajax call does not hitting the WEB API. I get the below error in browser console.

GET http://redrock.com:6606/api/values/get?callback=jQuery18207315279033500701_1383300951840&_=1383300951850 400 (Bad Request)

2个回答


您尚未包括用于路由设置的代码,但是假设它是正确的,则问题可能是由于您尝试使用POST请求来将其命名为WebApi方法“ Get”而导致的。 。 发生这种情况是因为WebApi试图从操作名称中找出HTTP动词。</ p>

我建议重命名操作或将 [HttpPost] </ code>属性添加到您的操作方法中。 您也可以尝试 WebApiRouteDebugger 程序包。</ p>
     </ div>

展开原文

原文

You haven't included the code for the route setup, but assuming it is correct, the problem is probably caused by the fact that you have named you WebApi method 'Get' while you are trying to hit it using a POST request. This happens because WebApi tries to figure out the HTTP verb from the action name.

I would suggest either renaming the action or adding the [HttpPost] attribute to your action method. You may also try the WebApiRouteDebugger package.


除非您要进行跨域调用,否则无需使用jsonp(jsonp在Web API中也需要自定义格式程序)。</ p>

  $ .getJSON('http://redrock.com:6606/api/values',函数(数据){
     console.log(data);
});
</ code> </ pre>

编辑:</ p>

要安装jsonp媒体类型格式化程序,请查看以下项目: https:// github .com / WebApiContrib / WebApiContrib.Formatting.Jsonp </ p>


  • 使用nuget下载格式化程序</ li>
  • 注册格式化程序</ li>
  • 更新您的routeconfig </ li>
    </ ul>
         </ div>

展开原文

原文

Unless you are doing a cross domain call, there is no need to use jsonp (jsonp also requires a custom formatter in Web API).

$.getJSON('http://redrock.com:6606/api/values', function(data){
    console.log(data);
});

EDIT:

To install a jsonp media type formatter, have a look at this project: https://github.com/WebApiContrib/WebApiContrib.Formatting.Jsonp

  • Download the formatter using nuget
  • Register the formatter
  • Update your routeconfig

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐