最近做了一個wcf項目,請求發起的項目是一個webform項目,所以這是分開的兩個項目端口必然不一樣,理所當然存在跨域問題。
有的人當下就反應過來jsonp,jsonp只能用於get請求,對於參數比較多的后台系統想想url后面掛着一排參數好像也不太美觀。
我找了一段時間的資料貌似在前端沒有處理空間了也就是放棄了在webform端處理跨域的這種辦法。
解決方案:在wcf端添加Golbal.asax文件然后
protected void Application_BeginRequest(object sender, EventArgs e) { HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*"); if (HttpContext.Current.Request.HttpMethod == "OPTIONS") { HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "POST, PUT, DELETE"); HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept"); HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000"); HttpContext.Current.Response.End(); } }