J'ai une fonction webservice HellowWorld (string str), et je veux l'appeler en js. Comment puis-je passer le paramètre dans HelloWorld à partir de js? Mon code est ci-dessous. Merci beaucoup.ajax paramètre à C# websevice
// code 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();
});
// code 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.
}
essayez de changer de type à POST. – daryal
Reportez-vous à [this] (http://stackoverflow.com/questions/5738097/jquery-ajax-with-json-call-to-wcf-service-pass-through-to-wcf-service) une fois, cela pourrait aider. – Raghuveer
Essayez avec le poste: http://stackoverflow.com/questions/11188315/javascript-array-as-wcf-webservice-parameter – Khurshid