简体   繁体   English

与ASP.NET网站通信时,jQuery AJAX返回错误0

[英]jQuery AJAX returning error 0 while communicating with ASP.NET site

I'm learning ASP.NET and I wanted to whip up a simple site that uses ajax to just get a string back from the server. 我正在学习ASP.NET,我想创建一个简单的网站,该网站使用ajax从服务器获取一个字符串。

When I run the ajax request it displays An error occured: 0 error 当我运行ajax请求时,显示An error occured: 0 error

Here's what I have so far 这是我到目前为止的

The controller for the index page with the ajax request on it: 带有ajax请求的索引页的控制器:

public ActionResult Index()
{
    return View();
}

The Controller for the ajax request: Ajax请求的控制器:

public class AjaxTestController : Controller
{
    public ActionResult AjaxAction()
    {
        return Content(@"Success!");
    }
}

the jQuery from the index page: 索引页面中的jQuery:

$( "#submit_button" ).click(function( event ) 
{
    $.ajax({
        type: "POST",
        url: "http://localhost:49636/AjaxTest/AjaxAction",
        data: "{}",
        success:function(result){
            alert( "Response: " + result );
        },
        error:function(xhr){
            alert("An error occured: " + xhr.status + " " + xhr.statusText);
    }});
});

When I go to http://localhost:49636/AjaxTest/AjaxAction in my browser it displays Success! 当我在浏览器中转到http://localhost:49636/AjaxTest/AjaxAction ,它显示Success! as expected. 如预期的那样。

Other questions about error 0 say it could have something to do with trying to access a different domain, but I'm not sure that applies here. 有关错误0的其他问题说,这可能与尝试访问其他域有关,但是我不确定在这里是否适用。 Any ideas? 有任何想法吗?

Thanks 谢谢

您是否尝试过将类型更改为“ GET”而不是“ POST”?

您还可以将[HttpPost] AjaxAction添加到AjaxAction

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM