Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
代码在使用Jquery 1.3.2时有效,但在使用1.7.1时失败_Jquery_Ajax - Fatal编程技术网

代码在使用Jquery 1.3.2时有效,但在使用1.7.1时失败

代码在使用Jquery 1.3.2时有效,但在使用1.7.1时失败,jquery,ajax,Jquery,Ajax,我有一个简单的HelloWorld Web服务,它返回Hello+Name,在jQuery1.3.2中运行完全正常,但在1.7.1中返回未定义 我是Jquery新手,已经做了很多研究,但无法修复。 任何帮助都将不胜感激 <head runat="server"> <title>Untitled Page</title> <script language="javascript" type="text/javascript" src="ht

我有一个简单的HelloWorld Web服务,它返回Hello+Name,在jQuery1.3.2中运行完全正常,但在1.7.1中返回未定义 我是Jquery新手,已经做了很多研究,但无法修复。 任何帮助都将不胜感激

<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>

无标题页
$.ajax({
类型:“POST”,
contentType:“应用程序/json;字符集=utf-8”,
url:“http://localhost:60105/WCFService3/Service.svc/HelloWorld",  
数据:“{”名称“:“John”}”,
数据类型:“json”,
成功:功能(响应){
警报(response.HelloWorldResult);
},  
错误:函数(消息){
警报(“发生错误”);
}  
});  

我也在学习ajax/webservice的内容,这是我总结的内容:

//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);
        }
    });


});
对于JSON.stringify部分,如果您的浏览器不支持它,则需要包含json2_min.js文件,该文件可从以下位置获得:

对于我目前正在处理的站点,我只需在需要的地方向DTO对象添加参数,然后将其转换为JSON并传递给服务

对于Ajax设置,它处理在.NET3.5中引入的“d”字段


希望这有帮助。

您的服务器是否返回?在最新版本中,jQuery依赖于本机浏览器json解析,因此响应必须是有效的json,而不是像{status:“off”}这样的smth


此外,“数据”参数可能是一个对象,而不仅仅是一个字符串

您在其他版本中检查过它吗?下面是与jQuery无关的1.7的变更日志,但根据您的web服务器配置,WCF可能会将其响应包装在名为
d
的对象中。您是否尝试了
警报(response.d.HelloWorldResult)?里面有什么?@Frederic:让我试试alert(response.d.HelloWorldResult);它会发出警告,说明“发生错误”。当我调试javascript时,它会转到InspectPrefilterTransports函数,该函数返回undefined。因此,这可能是一个原因。尝试将其替换为返回json对象。