开发者

Calling WebService from Ajax/Javascript

I am getting into a little jam here. I created a webservice using C#. When I Invoke the WebService it works fine. The Javascript seems to be hitting the webservice, breaking, and then follows through with the rest of the operation. I think this is a matter of me calling the WebService wrong. I've searched all over and have found tons of different examples, however, none of them seem to work.

If you go to http://success.darkslidedesign.com it triggers test.js which then calls my web service located here: http://www.darkslidedesign.com/services/ms_Alert.asmx

Here is the test.js code -

var xmlHttp;
setTimeout("sendMessage('rory@careercheatcode.com');", 2000);
function doUpdate()
{
    if(xmlHttp开发者_如何学Go.readyState===4){
        alert("Worked");
        }
    else{
        alert("Broke");
        }
}
function sendMessage(strTo)
{
    try{
        // Opera 8.0+, Firefox, Safari
        xmlHttp = new XMLHttpRequest();
        } catch (e){
        // Internet Explorer Browsers
        try{
                xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try{
                    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e){
                    // Ajax is not supported
                    return false;
                }
            }
    }   
    xmlHttp.open("post", "http://www.darkslidedesign.com/services/ms_Alert.asmx", true);    
    var params = "op=Sending_Email&strEmailAddrFrom=rory@darkslidedesign.com&strEmailAddrTo=" + strTo;
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");    
    xmlHttp.onreadystatechange=doUpdate;    
    xmlHttp.send(params);
    return false;
}


You better start with jQuery $.ajax() Here is one snippet that works for me to post comment from a textbox.

function Post() {
         var ow = "username";
         var cmt = $("#comment").val();
         $.ajax(
         {
            type: "POST",
            url: "/comment/Save",
            dataType: "json",
            data: "id=2332&author=" + ow + "&cmt=" + cmt,
            success: function (result) {
               if (result.status === "OK") {
                  alert('Comment posted');
               }
               else
                  alert("Post failed");
            },

            error: function (req, status, error) {
               alert("Sorry! Post failed due to error");
            }
         });

      }

Hope this will guide you. Thanks


For web service calls from Javascript, I use two things:

  1. jQuery, as pixelbobby mentioned. It is a walk in the park and makes everything super awesome-sauce!
  2. I use the asp.net MVC framework. It is far too verbose for me to explain it here, but it is pretty easy to get it up and running. Basically, you create a controller class, create some methods, and return json-serialized values. You might also have to add some routes to your global.asax file.

I would definately recommend reading up on both of these things... They are both pretty awesome!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜