1

我有一个 webservice 函数 HellowWorld(string str),我想在 js 中调用它。如何将参数从 js 传递到 HelloWorld?我的代码如下。非常感谢。

//js中的代码

function BindJson() {

    $.ajax({
        type: 'GET',
        url: 'Service1.asmx?op=HelloWorld',

        data: {str: 'asdasdas'},

        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data1) {
            alert('suc    ' + data1.d);
        },
        error: function (request, status, errorThrown) {
            alert('err    ' + status + errorThrown);
        }
    });
}

$(document).ready(function() {
    BindJson();    
});

//c#webservice中的代码

[WebMethod]
        public void HelloWorld(string str)
        {
            StreamWriter _testData = new StreamWriter(Server.MapPath("~/data.txt"), true);
            _testData.WriteLine(str); // Write the file.
            _testData.Flush();
            _testData.Close(); // Close the instance of StreamWriter.
            _testData.Dispose(); // Dispose from memory.       
        }
4

1 回答 1

1

?op= 是通过浏览器测试 web 服务的操作参数,您可以使用 'Service1.asmx/HelloWorld' 而不是 Service1.asmx/?op= HelloWorld

于 2012-09-04T07:08:56.577 回答