El código funciona cuando se usa Jquery 1.3.2 pero falla con 1.7.1

I have a simple HelloWorld webservice which returns Hello + Name and it works absolutely fine with Jquery 1.3.2 but returns undefined with 1.7.1 I am new to Jquery and have researched a lot but not able to fix. Any help will be appreciated.

<head runat="server">
    <title>Untitled Page</title>
    <script language="javascript" type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>

</head>
<body>
    <form id="form1" runat="server">

    <script language="javascript" type="text/javascript">
    $.ajax({  
      type: "POST",  
      contentType: "application/json; charset=utf-8",  
      url: "http://localhost:60105/WCFService3/Service.svc/HelloWorld",  
      data: '{"name":"John"}',  
      dataType: "json",  
      success: function(response) {  
      alert(response.HelloWorldResult);  
      },  
      error: function(message) {  
      alert("error has occured");  
      }  
    });  

    </script>
</body>

preguntado el 31 de enero de 12 a las 16:01

Have you checked it in any other version? blog.jquery.com/2011/11/03/jquery-1-7-released Heres the changelog for 1.7 -

Not related to jQuery, but depending on your web server configuration, WCF might wrap its response in an object named d. Has probado alert(response.d.HelloWorldResult);? -

Qué hay dentro console.log(response)? -

@Frederic: let me try alert(response.d.HelloWorldResult); -

It gives an alert saying "Error has occured". When I debug the javascript it goes down to the inspectPrefiltersOrTransports function which is returning undefined. hence the error -

2 Respuestas

I'm just learning ajax / webservice stuff as well and this is what I've put together:

//Create an empty DTO (Data Transfer Object)
var DTO = {};

$(function () {
    // set Ajax default settings
    $.ajaxSetup({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        data: "{}",
        dataType: "text", //do NOT set to json!!!
        converters: {
            "text json": function (jsonString) {
                var res = JSON.parseWithDate(jsonString);
                if (res && res.hasOwnProperty("d"))
                    res = res.d;
                return res;
            }
        },
        dataFilter: function (data) {
            var msg;
            if (typeof (JSON) !== 'undefined' && typeof (JSON.parse) === 'function')
                msg = JSON.parse(data);
            else
                msg = eval('(' + data + ')');

            if (msg.hasOwnProperty('d'))
                return msg.d;
            else
                return msg;
        }
    });

    //Set the parameter of the DTO  
    DTO.name = "John";

    $.ajax({
        url: location.protocol + "//" + location.host + "/WCFService3/Service.svc/HelloWorld",
        data: JSON.stringify(DTO),
        success: function (data) {
            alert(data.HelloWorldResult);
        },
        error: function (xhr, err, desc) {
            alert(jQuery.parseJSON(xhr.responseText).Message);
        }
    });


});

for the JSON.stringify part, if your browser doesn't support it you will need to include the json2_min.js file available from:

http://www.json.org/js.html

For the site I'm currently working on, I simply add parameters to the DTO object where needed which will then be turned into JSON and passed to the service.

For the Ajax setup, that deals with the "d" field, which was introduced in .NET 3.5

Espero que esto ayude.

Respondido el 31 de enero de 12 a las 22:01

was is your server returning? In the latest version jQuery relies on native browser json parsing, so that the response has to be valid json, and not smth like '{status:"off"}'.

Also, the "data" parameter may be an object, not just a string

Respondido el 31 de enero de 12 a las 23:01

That is probably a reason. Try replacing it with returning a json object. - Caramelos Gratis

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.