【问题标题】:Asp.Net Mvc 3, jQuery, cross-domainAsp.Net Mvc 3、jQuery、跨域
【发布时间】:2012-01-24 08:33:38
【问题描述】:

我有一个站点“A”并使用 jQuery 测试站点“B”的跨站点帖子,我已将其添加到 Global.asax Application_BeginRequest

HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "X-Requested-With");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "POST");

站点“B”的帖子如下所示:

$.ajax({
    type: 'POST',
    url: rootUrl,
    crossDomain: true,
    data: request.toPostData(),
    dataType: 'json',
    success: onsuccess,
    error: onerror
});

我现在的问题是它允许对控制器操作进行两个请求。一个有表单值,一个没有。

当然,我只想要最后一个,它包含值。我希望任何人都可以解释并指出我的解决方案。

【问题讨论】:

    标签: asp.net-mvc-3 jquery cross-domain


    【解决方案1】:

    要跨域,您需要使用 jsonp http://en.wikipedia.org/wiki/JSONP 之类的东西

    【讨论】:

    • 请求确实通过了数据。
    【解决方案2】:

    使用 dataType:'jsonp' 并在你的 url 后面加上 '&callback=?'

    $.ajax({
        type: 'POST',
        dataType:'jsonp',
        url: rootUrl,
        crossDomain: true,
        data: request.toPostData(),
        success: onsuccess,
        error: onerror
    });
    

    更多参考http://www.ibm.com/developerworks/library/wa-aj-jsonp1/

    【讨论】:

    • 只给jQuery151042722045979462564_1327352766794 was not called
    猜你喜欢
    • 2011-08-23
    • 1970-01-01
    • 2012-07-28
    • 2016-08-10
    • 2011-01-31
    • 1970-01-01
    • 2011-07-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多