0

I'm trying to call a webservice using ajax, im getting a internal 500 ERROR.Can you please advise im not sure what im doing wrong, i can call a webmethod with no problem.

JQUERY AJAX CALL

<script type="text/javascript">

function LoginVailid() {

    $.ajax({
        url: "http://localhost:49301/AppService.asmx/LoggonAuthentication",
        contentType: "application/json; charset=utf-8",
        data: "{}",
        dataType: "jsonp",
        jsonp: "callback",
        crossDomain: true,
        success: function (json) {
            alert(json.d);
        },
        error: function () {
            alert("Hit error fn!");
        }
    });
}
</script> 

WEBSERVICE METHOD

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]

[System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{

    [WebMethod]
    [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
    public string LoggonAuthentication()
    {
        return "Hello World";
    }

}
  • Can you try change `dataType` from `jsonp` to `json` – Max Brodin Nov 06 '14 at 16:10
  • If i do that then a get the following ERROR.XMLHttpRequest cannot load http://localhost:49301/AppService.asmx/LoggonAuthentication?{}. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:55255' is therefore not allowed access. The response had HTTP status code 500. – Alfred Alfizo Mosima Nov 06 '14 at 16:11
  • > I'm trying to call a webservice using ajax, im getting a internal 500 ERROR. Can you debug and check the real exception? – Max Brodin Nov 06 '14 at 16:17
  • http://learn.jquery.com/ajax/working-with-jsonp/ The advent of JSONP — essentially a consensual cross-site scripting hack — has opened the door to powerful mashups of content. Many prominent sites provide JSONP services, allowing you access to their content via a predefined API. – Alfred Alfizo Mosima Nov 06 '14 at 16:23
  • Check this [SO question](http://stackoverflow.com/questions/19174435/how-to-add-access-control-allow-origin) – Max Brodin Nov 06 '14 at 16:26

1 Answers1

0

Try this code

[WebMethod]
[ScriptMethod(UseHttpGet = true, XmlSerializeString=false, ResponseFormat = ResponseFormat.Json)]
public string LoggonAuthentication(string callback)
{
    return  callback + "({message: 'Hello World'})";
}

Also this article and this SO question can help

Community
  • 1
  • 1
Max Brodin
  • 3,903
  • 1
  • 14
  • 23