2015-09-06 43 views
0

我有MVC應用程序,我們的客戶使用我們提供的代碼使用JSONP ajax調用從他們的網站發送他們的請求。會話不能在IE和Mobile Safari中使用JSONP調用ASP.MVC

當我們在Chrome和Firefox上瀏覽他們的網站時,MVC應用程序可以保留會話信息,但是當您從IE瀏覽器(任何版本)瀏覽網站時,移動Safari MVC應用程序會將每個請求視爲新請求。相信這是前端或後端的問題(因爲一切都在Chrome中工作正常,FF我認爲這是前端相關)

這裏是代碼:

在我們的web.config文件中,我們有這個允許CORS從任何地方:

<httpProtocol> 
    <customHeaders> 
    <add name="Access-Control-Allow-Origin" value="*" /> 
    <add name="Access-Control-Allow-Headers" value="Content-Type, Accept" /> 
    </customHeaders> 
</httpProtocol> 

這是我們爲我們的客戶提供的JSONP通話功能

$.ajax({ 
    type: "GET", 
    url: "<OUR API>?callback=?", 
    data: { 
     <OUR VARIABLES> 
    }, 
    beforeSend: setHeader, 
    xhrFields: { 
     withCredentials: true 
    }, 
    dataType: "jsonp", 
    success: function (data) {} 
}) 

function setHeader(xhr) { 
    xhr.setRequestHeader('Access-Control-Allow-Origin', '*'); 
} 

這是處理後端

public class ClientController : Controller, IRequiresSessionState 
{ 
    // 
    // GET:/
    [AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)] 
    public string Index() 
    { 
     if (System.Web.HttpContext.Current.Session["test-var"] != null) 
      return "you are here before " + DateTime.Now.ToString("MM/dd/yyyy HH:mm"); 
     System.Web.HttpContext.Current.Session["test-var"] = "hi"; 
     return "you are new"; 
    } 

回答

0

你嘗試添加屬性[的WebMethod(EnableSession =真)]的C#代碼

public class ClientController : Controller, IRequiresSessionState 
{ 
// 
// GET:/
[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)] 
[WebMethod(EnableSession = true)] 
public string Index() 
{ 
    if (System.Web.HttpContext.Current.Session["test-var"] != null) 
     return "you are here before " + DateTime.Now.ToString("MM/dd/yyyy HH:mm"); 
    System.Web.HttpContext.Current.Session["test-var"] = "hi"; 
    return "you are new"; 
} 
+0

剛剛嘗試過,沒有運氣!我認爲它與會話Cookie有關,不知何故,Chrome和FF讓MVC應用程序創建會話cookie,而IE不能這樣做,不知道.. –