經常遇到這個問題,每次都要再查一下是爲什麼,還是記錄一下吧
有兩個可能:
1.前端請求:需要設置:
header:{
"content-type":"application/x-www-form-urlencoded; charset=UTF-8"
},
2.接口問題:訪問web服務,要對web服務添加修飾:[System.Web.Script.Services.ScriptService] 否則,當.ajax()請求服務時,會有異常:只能從腳本中調用在類定義上有[ScriptService]屬性的 Web 服務
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//若要允許使用 ASP.NET AJAX 從腳本中調用此 Web 服務,請取消對下行的註釋。
[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService {
public WebService () {
//如果使用設計的組件,請取消註釋以下行
//InitializeComponent();
}
[WebMethod]
public string HelloWorld() {
return "Hello World";
}
}