Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
跨域$.ajax和404错误_Ajax_Asp.net Mvc - Fatal编程技术网

跨域$.ajax和404错误

跨域$.ajax和404错误,ajax,asp.net-mvc,Ajax,Asp.net Mvc,我想使用$.ajax从外部ASP.NET MVC站点(在本例中是从我的站点)检索一些数据。下面的代码给我一个404未找到错误(当然url是有效的) 但是,如果我将url从url:'http://myurl.com/Home/GetMyCode/?id=mycode“tourl:”http://localhost:123/Home/GetMyCode/?id=mycode“一切正常。那么,如何修复它呢 $.ajax({ url: 'http://myurl.com/Home/Ge

我想使用
$.ajax
从外部ASP.NET MVC站点(在本例中是从我的站点)检索一些数据。下面的代码给我一个
404未找到错误
(当然
url
是有效的)

但是,如果我将url从
url:'http://myurl.com/Home/GetMyCode/?id=mycode“
to
url:”http://localhost:123/Home/GetMyCode/?id=mycode“
一切正常。那么,如何修复它呢

 $.ajax({
        url: 'http://myurl.com/Home/GetMyCode/?id=mycode',
        type: 'POST',
        contentType: "application/json; charset=utf-8",
        crossDomain: true,
        success: function (res) {
            ...
        },
        error: function (jqXHR, textStatus, errorThrown) {
            ...
        }
    });


    [HttpPost]
    public JsonResult GetMyCode(string id)
    {
        try 
        {
            return Json(new { result = "ok", resultData = "OK") });
        }
        catch (Exception e)
        {
            return Json(new { result = "error", resultData = "An error occured" });
        }            
    }

看起来可能是DNS问题。您是否能够访问:


您试图访问的.com域是否可公开访问?或者它是本地主机的环回?

为了跨域检索数据,您可能需要使用“jsonp”

两种方法来处理跨域Ajax调用:

JSONP:跨域访问的当前标准

JSONP是一些站点使用的一种约定,用于以一种方式公开其内容,使调用者更容易通过脚本使用数据,即使来自外部域。诀窍在于让站点返回一些JSON内容,而不是以普通字符串形式返回,而是封装在脚本函数调用中。有关详细信息

跨来源资源共享(CORS)

要在不支持cors但允许跨域XHR请求(windows gadget等)的环境中启用跨域请求,请设置
$.support.cors=true;

您只需告诉jQuery您所处的环境可以进行跨域XHR请求。

该教程对我很有用,我必须在我的MVC项目中实现JSONP处理。

myurl.com是否接受端口80上的web流量?如果不是,则需要在url中指定。()