0
I am making a web service in c# , to consume its data via javascript (without being an application made in Asp.net or any technology . NET ,I want to use html and javascript only).
My web service was configured like this:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class PhoneGap : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
}
And my ajax was like this :
function GetData() {
$.ajax({
type: 'POST'
//Caminho do WebService + / + nome do metodo
, url: "http://localhost:3458/PhoneGap.asmx/HelloWorld"
, contentType: 'application/json; charset=utf-8'
, dataType: 'json'
//Abaixo adicione as variáveis caso haja alguma.
, data: ''
, success: function (data, status) {
//Caso não ocorra nenhum tipo de erro:
$('.valor').text(data.d);
}
, error: function (xmlHttpRequest, status, err) {
//Caso ocorra algum erro:
$('.valor').html('Ocorreu um erro');
}
});
}
When I am going to take the test to consume the web service Hello Word is giving error(Note: Ententei thinks if I could use the web service locally mind for testing but I did not find anything using html and ajax apeans)
Error:
Failed to load http://localhost:3458/Phonegap.asmx/Helloworld: Response to preflight request doesn’t pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested Resource. Origin 'null' is therefore not allowed access.
Possible duplicate of CORS - No 'Access-Control-Allow-Origin' header is present on the requested Resource
– Homer Simpson