本文介绍了将json对象字符串数组发送到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想把以下的json发给serever



i want to post the follwing json to serever

{"jsonStr":[{"empid":10,"name":"Kev","dept":32}],"path":"D://koko.txt"}





我的代码如下:




my code is as follwing

var $empId   = uniqId();
               var $empName = $("#empName").val();
               var $deptId = $("#dept").val();
               var emp1 = new emp($empId, $empName, $deptId);
               people.push(emp1);







obj.jsonStr = people;

               obj.path = "D://koko.txt";







$.ajax({
                    type: "POST",
                    url: "Save.aspx/WriteJSON",
                    contentType: "application/json; charset=utf-8",
                    data: JSON.stringify(obj),
                    dataType: "json",
                    success: function (r) {
                        alert(r.d)
                    },
                    error: function (x, e) {
                        if (x.status == 0) {
                            alert('You are offline!!\n Please Check Your Network.');
                        } else if (x.status == 404) {
                            alert('Requested URL not found.');
                        } else if (x.status == 500) {
                            alert('Internel Server Error.');
                        } else if (e == 'parsererror') {
                            alert('Error.\nParsing JSON Request failed.');
                        } else if (e == 'timeout') {
                            alert('Request Time out.');
                        } else {
                            alert('Unknow Error.\n' + x.responseText);
                        }
                    }
                });//end post







以下退回






the following is returned

Internal Server Error





服务器代码



server code

public static string WriteJSON(string jsonStr, string path)
    {

        FileStream file = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
        file.SetLength(0);
        StreamWriter sr = new StreamWriter(file);
        // Read contents of file into a string
        sr.Write(jsonStr);
        sr.Close();
        // Close file
        file.Close();
        return "ok";
    }





什么是解决方案



what is the solution

推荐答案




这篇关于将json对象字符串数组发送到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 07:58