我正在尝试从本地计算机调用 Web 服务。但是我在 Chrome 控制台中收到以下错误:
http://www.test.com/service.svc/api/?cid=1 405 (Method Not Allowed)
XMLHttpRequest cannot load http://www.test.com/service.svc/api/?cid=1.
Origin http://localhost is not allowed by Access-Control-Allow-Origin.
我的本地测试网址是:
http://localhost/welcome/html/index.html
当我将我的代码上传到实际域并从那里调用网络服务时,它当然可以工作。
我已经尝试更改访问控制标头,但这无济于事。
Namespace RestService
Public Class service
Implements Iservice
Public Function GetProvinces(ByVal cid As String) As AjaxControlToolkit.CascadingDropDownNameValue() Implements Iweddingservice.GetProvinces
WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Methods", "DELETE, POST, GET, OPTIONS")
WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Origin", "*")
Dim MyConnection As SqlConnection = GetConnection()
Dim cmd As New SqlCommand("SELECT provinceid,title FROM provinces WHERE CountryID=@CountryID ORDER BY title ASC", MyConnection)
cmd.Parameters.Add(New SqlParameter("@CountryID", cid))
Dim values As New List(Of CascadingDropDownNameValue)
Try
MyConnection.Open()
Dim reader As SqlDataReader = cmd.ExecuteReader
While reader.Read
values.Add(New CascadingDropDownNameValue(reader("title").ToString, reader("provinceid").ToString))
End While
Catch ex As Exception
Finally
MyConnection.Close()
End Try
Return values.ToArray
End Function
End Class
End Namespace