I'm trying to call a webservice from my local machine. But the I get the following errors in Chrome console:
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.
My local test URL is:
http://localhost/welcome/html/index.html
When I upload my code to the actual domain and call the webservice from there, it does work ofcourse.
I've already tried to change the access control headers, but that doesnt help.
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
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…