调用Web服务时json .responseText未定义 [英] json .responseText undefined when calling web service

查看:76
本文介绍了调用Web服务时json .responseText未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取基本的json网络服务以返回数据并继续遇到相同的问题.返回的json对象似乎不包含任何数据.

这是我的网络服务:

I am trying to get a basic json web service to return data and keep running into the same problem. The json object returned does not seem to contain any data.

Here is my web service:

[WebMethod(Description = "This is a test JSON service", EnableSession = false)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string testJSON(string a, string b, string c, string d, string e, string f)
{
    string temp = "Data " + a + " " + b + " " + c + " " + d + " " + e + " " + f;

    //yourobject is your actula object (may be collection) you want to serialize to json
    DataContractJsonSerializer serializer = new DataContractJsonSerializer(temp.GetType());
    //create a memory stream
    MemoryStream ms = new MemoryStream();
    //serialize the object to memory stream
    serializer.WriteObject(ms, temp);
    //convert the serizlized object to string
    string jsonString = Encoding.Default.GetString(ms.ToArray());
    //close the memory stream
    ms.Close();
    return jsonString;

    //return temp;
}



当为参数输入s d f和g时,返回jsonString的格式为"\" Data a s d f g h \".在.net测试窗口中,此方法工作正常,但仅返回xml,而不返回json(据我了解,.net测试仅返回xml.是这样吗?您可以从测试页中看到json吗?)

这是我尝试调用此服务的页面:



The return jsonString is in the format "\"Data a s d f g h\"" when a s d f and g are entered for the parameters. This works fine in the .net testing window, but only returns xml, not json (it is my understanding that the .net test only ever returns xml. Is that true? Can you see json from the test page?)

Here is my page where I try to call this service:

%<@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
    <script src="jquery-1.3.1.js" type="text/javascript"></script>
    <script type="text/javascript">
        function getProducts(a,b,c,d,e,f) {
            $('#divResults').hide();
            $('#divLoading').show();
            try {
                    $.ajax({
                        type: "POST",
                        url: /Service1.asmx/testJSON",
                        data: "{'a' : 'a', 'b' : 's', 'c' : 'd', 'd' : 'f', 'e' : 'g', 'f' : 'h'}",
                        contentType: "application/json; charset=utf-8",
                        success: ajaxCallSucceed,
                        dataType: "json",
                        failure: ajaxCallFailed
                    });
                    //JsonWebServiceWithJQuery/jsonwebservice.asmx?op=GetProducts
            }
            catch (e) {
                alert('failed to call web service. Error: ' + e);
                $('#divLoading').hide();
            }
        }
        function ajaxCallSucceed(response) {
            $('#divLoading').hide();
            alert("Succeed");
//            var products = eval('(' + response.d + ')');
            alert(response.responseText);
            alert(response.toString());
       }
        function ajaxCallFailed(error) {
            $('#divLoading').hide();
            alert('error: ' + error);
            $('#divResults').hide();
        }
    </script>


alert(response.responseText)返回未定义
alert(response.toString())返回[object Object]

任何帮助将是非常非常感谢.几天来,我一直在用头撞在墙上,但没有任何进展.


alert(response.responseText) is returning undefined
alert(response.toString()) is returning [object Object]

Any help would be very very appreciated. I have been banging my head against the wall on this one for days now and am making no progress.

Thanks!

推荐答案

('#divResults').hide();
('#divResults').hide();


('#divLoading').show( ); 尝试{
('#divLoading').show(); try {


.ajax({ 输入:"POST", 网址:/Service1.asmx/testJSON", 数据:"{'a':'a','b':'s','c':'d','d':'f','e':'g','f':'h '}", contentType:"application/json; charset = utf-8", 成功:ajaxCallSucceed, dataType:"json", 失败:ajaxCallFailed }); //JsonWebServiceWithJQuery/jsonwebservice.asmx?op=GetProducts } 抓住(e){ alert('无法调用Web服务.错误:'+ e);
.ajax({ type: "POST", url: /Service1.asmx/testJSON", data: "{'a' : 'a', 'b' : 's', 'c' : 'd', 'd' : 'f', 'e' : 'g', 'f' : 'h'}", contentType: "application/json; charset=utf-8", success: ajaxCallSucceed, dataType: "json", failure: ajaxCallFailed }); //JsonWebServiceWithJQuery/jsonwebservice.asmx?op=GetProducts } catch (e) { alert('failed to call web service. Error: ' + e);


这篇关于调用Web服务时json .responseText未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆