6
I have a project in Asp.Net MVC with the following method:
public ActionResult ObterCursos()
{
    List<curso> cursos = new List<curso>();
    curso curso_ = new curso();
    curso_.Nome = "Análise";
    curso_.Periodo = 3;
    curso_.qtdSemestre = 5;
    curso_.Codigo = "ADS";
    cursos.Add(curso_);
    curso_ = new curso();
    curso_.Nome = "Ciencia da Computação";
    curso_.Periodo = 3;
    curso_.qtdSemestre = 8;
    curso_.Codigo = "CDC";
    cursos.Add(curso_);
    return Json(cursos, JsonRequestBehavior.AllowGet);
}
As you can see, it returns a Json as a result. I am running such project on my local machine.
In another project, I am trying to make an Ajax request to the method created in the project previously described.
$.ajax({
    url : "http://localhost:10642/Mobile/ObterCursos",
    type : "GET",
    dataType : "json",
    success : function(dados) {
        resultado = dados;
        alert("json sucess")
    },
    error : function(xhr, ajaxOptions, error) {
        alert("erro json: " + xhr.responseText)
    }
});
Upon execution, the alert of error is shown and the following error is displayed on the console:
Xmlhttprequest cannot load http://localhost:10642/Mobile/Get courses. No 'Access-Control-Allow-Origin' header is present on the requested Resource. Origin 'null' is therefore not allowed access.
Conducting my research, I discovered I should change the dataType of Ajax for jsonp. And so I did, but alert of error is still shown, however the error that was displayed on console no longer appears.
Can someone help me?
This here:
url : "http://localhost:10642/Mobile/ObterCursos",is bad practice. Switch to:/Mobile/ObterCursos.– Leonel Sanches da Silva
The problem @Ciganomorrisonmendez is that as I said in the problem, Ajax is running on another project.
– Jedaias Rodrigues
It’s still bad practice. The correct thing is that the beginning of the address comes from
Web.config.– Leonel Sanches da Silva
Well @Ciganomorrisonmendez the problem is that in my project where Ajax is a project
Cordova, in this case how I could set up theWeb.config?– Jedaias Rodrigues
I think the ideal would be to open a question just for that. They are a few steps, but it is pertinent the separation.
– Leonel Sanches da Silva
Opa @Ciganomorrisonmendez opened the question well here.
– Jedaias Rodrigues