Question

I am trying to call a asmx service from an HTML page through JavaScript and jQuery. Here is my code for the service HelloWorldTest.asmx:

[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 HelloWorldTest : System.Web.Services.WebService
{

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true)]
    public string HelloWorld5()
    {
        string connetionString = null;
        SqlConnection connection;
        SqlDataAdapter adapter = new SqlDataAdapter();
        string sql = null;
        connetionString = System.Configuration.ConfigurationManager.ConnectionStrings["pollConString"].ToString();
        Random _rg = new Random();
        connection = new SqlConnection(connetionString);
        sql = "insert into Country (OID,country_name) values( 'Servicecalled-" + Convert.ToString(_rg.Next(0, 99999999)).PadLeft(8, '0') + "','"+System.DateTime.Now.ToString() +"')";

        connection.Open();
        adapter.InsertCommand = new SqlCommand(sql, connection);
        adapter.InsertCommand.ExecuteNonQuery();  
        return "Helloworld";
    }
}

I have published the service in a local server, which is running in 192.168.0.124 and port 80.

Now here is my HTML page code for the client call:

$.ajax({
    type: 'GET',
    url: http://192.168.0.124:80/pollservice/Services/HelloWorldTest.asmx/HelloWorld5',
    processData: true,
    data: {},               
    dataType: "json; charset=utf-8",
    responseType: "",
    success: function (data, textStatus, jqXHR) {
        processData(data, textStatus, jqXHR);
    }
});

function processData(data, textStatus, jqXHR) {                  
    alert(' data d = ' + data.d); 
}

Now the issue:

when I am running into the localhost I get a return from the service. It is a simple string. But, when I am publishing it into the server in a LAN and I am invoking from client machine, then I am getting null output.

But, Interestingly, the log is written into the server. So the Helloworld5() method is called from the JavaScript method through $.ajax(....) call . But the JSON return data is empty in the case of the published server.

Why this is happening? I am running the published website in Asp.Net integrated mode.

Was it helpful?

Solution

This could be because of the Same Origin policy if the html and the asmx service are running in different servers. To enable cross origin ajax requests, you have to use JSONP or CORS.

To ensure that this is a cross origin issue you can use the chrome developer tools, where you will get a message "Origin is not allowed by Access-Control-Allow-Origin" in case of cross origin issues.

I had a similar issue and i ended up using JSONP.

OTHER TIPS

you're declaring the return type as JSON, but actually returning a naked string. you probably want to build the JSON data before returning it, e.g.:

// ...

string responseData = "{ d : \"";
responseData += "Hello World!";
responseData += "\" }";

return responseData;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top