2012-09-04 1 views
1

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.  
     } 
+0

essayez de changer de type à POST. – daryal

+0

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

+0

Essayez avec le poste: http://stackoverflow.com/questions/11188315/javascript-array-as-wcf-webservice-parameter – Khurshid

Répondre

0

? Op = est paramètre opérationnel pour les tests webservice par navigateur, vous pouvez aller avec « Service1.asmx/HelloWorld 'au lieu de Service1.asmx /? op = HelloWorld

+0

J'ai essayé ceci mais obtenez le message "erreur interne de serveur" qui signifie se produisant l'erreur: fonction. – Zmoey