我想通過JavaScript和jQuery從HTML頁面調用asmx服務。 這裏是我的服務HelloWorldTest.asmx代碼:jQuery跨站調用到ASMX服務返回值null
[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";
}
}
我已經發表在本地服務器上的服務,這是在192.168.0.124運行和端口80
現在,這裏是我的HTML頁面客戶端調用代碼:
$.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);
}
現在的問題:
當我運行到本地主機我得到的服務回報。這是一個簡單的字符串。但是,當我將它發佈到局域網中的服務器上時,我正在從客戶端機器調用,然後我得到空輸出。
但是,有趣的是,日誌被寫入服務器。所以Helloworld5()方法通過$ .ajax(....)調用從JavaScript方法調用。但是對於發佈的服務器,JSON返回數據是空的。
爲什麼會發生這種情況?我以Asp.Net集成模式運行已發佈的網站。
謝謝艾琳。我的格式JSON字符串作爲您的意見。但仍然沒有進展。期待這個問題... – Ashish 2012-04-07 07:25:06