jQuery的Ajax响应始终没有返回值返回值、jQuery、Ajax

2023-09-11 00:43:39 作者:在時光深處躲貓貓

下面code返回一个空的响应无论与否的功能存在,甚至是Web服务的文件全部:

The following code returns a blank response no matter whether or not the Function exists, or even the Web Service file entirely:

$.ajax({
    url: "/ws.asmx/HelloWorld"
    , type: "POST"
    , contentType: 'application/json; charset=utf-8'
    , data: '{ FileName: "' + filename + '" }'
    , dataType: 'json'
    , success: function (data) {

    }
});

这是为什么?

Why is this?

此外,可能值得注意, $。负载()工作正常!

Also, might be worth noting, $.load() works fine!

推荐答案

您错误是试图手动构建JSON数据,并为此在错误的方式:

Your error is that you try to construct JSON data manually and do this in the wrong way:

'{ FileName: "' + filename + '" }'

您应该可以解决code。至少以下

You should fix the code at least to the following

'{ "FileName": "' + filename + '" }'

由于对应于 JSON规范属性名也必须是双引号括起来。

because correspond to the JSON specification the property names must be also double quoted.

接下来的问题你能有如果文件名有一些特殊的字符。例如,在情况下的

Next problem can you has if the filename has some special characters. For example, in case of

var filename = '"C:\\Program Files"'; // the '\' must be escaped in the string literal

您应该具有与数据对应的JSON字符串

you should has as the data the corresponding JSON string

'{ "FileName": "\\"C:\\\\Program Files\\"" }'

作为,因为'\'和''的对应JSON数据的必须进行转义。它看起来难治,所以我的 stricly推荐您构建JSON字符串相对于的 JSON.stringify 从的 json2.js 。然后,code将

as the corresponding JSON data because of '\' and '"' must be escaped. It looks dificult. So I stricly recommend you to construct JSON strings with respect of JSON.stringify function from the json2.js. Then the code will be

$.ajax({
    type: "POST",
    url: "ws.asmx/HelloWorld",
    data: JSON.stringify({ FileName: filename }),
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (data) {
        alert(data.d);
    },
    error: function (XMLHttpRequest, textStatus, errorThrown) {
        alert("Error Occured!" + " | " + XMLHttpRequest.responseText +
               " | " + textStatus + " | " +  errorThrown);
    }
});

这是简单而清楚。用法的下一个优势 JSON.stringify 是最现代化的网络浏览器拥有的本地支持的功能和作用的工作非常快。

which is simple and clear enough. The next advantage of the usage JSON.stringify is that the most modern web browsers has native support of the function and the function work very quickly.

通过的情况下的使用方式 JSON.stringify 你可以很容易调用Web服务收作方法初探有着非常复杂的数据结构(类)作为参数,不仅弦

By the way in case of the usage of JSON.stringify you can easy call web service methd having very complex data structures (classes) as the parameters and not only strings.

更新时间::还有一个remrk,以减少可能的误解。这以后deceide使用HTTP GET而不是HTTP POST调用Web方法,你将不得不修改数据

UPDATED: One more remrk to reduce possible misunderstanding. It you later deceide to use HTTP GET instead of HTTP POST to call the web method you will have to change the data parameter from

JSON.stringify({ FileName: filename })

{ FileName: JSON.stringify(filename) }

更新2 :您可以下载这个 Visual Studio中,我用来测试的所有之前,我贴我的回答2010项目。我列为网络3.5.config的web.config中的.NET 3.5。列入的default.htm工作的所有不同的评论数据值。如果你想使用HTTP测试得到你应该注释web.config中的部分,它允许HTTPGET,并使用 ScriptMethod UseHttpGet = TRUE 。所有的线都包括在演示作为注释

UPDATED 2: You can download this Visual Studio 2010 project which I used to test all before I posted my answer. I included as "Web-3.5.config" the web.config for .NET 3.5. All different commented data values included in the default.htm work. If you want make tests with HTTP GET you should uncomment the section in web.config which allows HttpGet and use ScriptMethod having UseHttpGet = true. All the lines are included in the demo as comments.

 
精彩推荐