Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.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
C# Json成功未在有效Json上触发_C#_Jquery_Json - Fatal编程技术网

C# Json成功未在有效Json上触发

C# Json成功未在有效Json上触发,c#,jquery,json,C#,Jquery,Json,我正在尝试将json消息从自定义应用程序发送到网页。正在返回json消息,该消息似乎有效,但不会触发success函数。我目前正在使用jquery-1.4.4.js,之前我使用jquery.com上托管的jquery-latest.js。使用该版本时,我收到了一个parseerror错误,但使用1.4.4时,我没有收到成功或失败的消息 我的代码如下: 在网页上: <script> $(document).ready(function () { $.ajax({url: "ht

我正在尝试将json消息从自定义应用程序发送到网页。正在返回json消息,该消息似乎有效,但不会触发success函数。我目前正在使用jquery-1.4.4.js,之前我使用jquery.com上托管的jquery-latest.js。使用该版本时,我收到了一个parseerror错误,但使用1.4.4时,我没有收到成功或失败的消息

我的代码如下:

在网页上:

<script>
$(document).ready(function () {
    $.ajax({url: "http://localhost:8080/callback=?",

            dataType: "json",
            success: function(data) 
            {
                alert("reply received");
            },
            error: function(data, error) 
            {
                alert("error: " + error);
            }
        });
});
</script>
在我的C应用程序中,代码是:

string response = "[{\"name\":\"John\"},{\"name\":\"Mike\"}]";

request.ContentType = "application/json";
webserver.SendToBrowser(response, request);

public void SendToBrowser(string data, Classes.HTTPRequest request)
    {
        int numBytes = 0;
        byte[] bData = Encoding.ASCII.GetBytes(data); 
        try
        {
            string header = "";
            header += "HTTP/1.1 200 OK\r\n";
            header += "Server: MyServer\r\n";
            header += "Content-Length: " + bData.Length.ToString() + "\r\n";
            header += "Content-Language: en\n\r";
            header += "Content-Type: " + request.ContentType + "\r\n";
            header += "Connection: close\r\n\r\n";

            Byte[] headerBytes = Encoding.ASCII.GetBytes(header);

            if (request.Socket.Connected)
            {

                request.Socket.Send(headerBytes, headerBytes.Length, 0);

                if ((numBytes = request.Socket.Send(bData, bData.Length, 0)) == -1)
                    Console.WriteLine("Socket Error cannot Send Packet");
                else
                {
                    Console.WriteLine("No. of bytes sent {0}", numBytes);
                }
            }
            else
                Console.WriteLine("Connection Dropped....");
        }
        catch (Exception  e)
        {
            Console.WriteLine("Error Occurred : {0} ", e );
        }
        request.Socket.Close();
    }
非常感谢任何帮助

JSONP不是魔法


您的服务器需要遵循JSONP协议并读取callback=参数。

是的,您是对的,我需要读取回调函数,然后将参数名称传递回页面。e、 使用callback=?将生成callback=jsonp1341571525854的调用,然后我需要将其作为jsonp1341571525854[{name:John},{name:Mike}]返回到json消息中的页面
public void SendToBrowser(string data, Classes.HTTPRequest request)
    {
        int numBytes = 0;
        byte[] bData = Encoding.ASCII.GetBytes(data); 
        try
        {
            string header = "";
            header += "HTTP/1.1 200 OK\r\n";
            header += "Server: MyServer\r\n";
            header += "Content-Length: " + bData.Length.ToString() + "\r\n";
            header += "Content-Language: en\n\r";
            header += "Content-Type: " + request.ContentType + "\r\n";
            header += "Connection: close\r\n\r\n";

            Byte[] headerBytes = Encoding.ASCII.GetBytes(header);

            if (request.Socket.Connected)
            {

                request.Socket.Send(headerBytes, headerBytes.Length, 0);

                if ((numBytes = request.Socket.Send(bData, bData.Length, 0)) == -1)
                    Console.WriteLine("Socket Error cannot Send Packet");
                else
                {
                    Console.WriteLine("No. of bytes sent {0}", numBytes);
                }
            }
            else
                Console.WriteLine("Connection Dropped....");
        }
        catch (Exception  e)
        {
            Console.WriteLine("Error Occurred : {0} ", e );
        }
        request.Socket.Close();
    }