Click here to Skip to main content
15,896,207 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

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


CSS
obj.jsonStr = people;
               
               obj.path = "D://koko.txt";


C#
$.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
Posted
Updated 2-Feb-15 16:29pm
v2
Comments
ducksbabariya 2-Feb-15 23:35pm    
You need to pass jsonStr and path separately bcoz, while request posts data there is jsonStr is list of emp's but server side it requires as string...
Please see below answer to make it working.

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

Code should be as below :
var data = [];
var obj = {};
obj.empId = uniqId();
obj.empName = $("#empName").val();
obj.deptId = $("#dept").val();
data.push(obj);
var jsonStr = JSON.stringify(data)
var path = "D://koko.txt"

$.ajax({
type: "POST",
url: "Save.aspx/WriteJSON",
contentType: "application/json; charset=utf-8",
data: {jsonStr : jsonStr,path :path },
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('Unkn

1 solution

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

Code should be as below :
var data = [];
var obj = {};
obj.empId = uniqId();
obj.empName = $("#empName").val();
obj.deptId = $("#dept").val();
data.push(obj);
var jsonStr = JSON.stringify(data)
var path = "D://koko.txt"

$.ajax({
type: "POST",
url: "Save.aspx/WriteJSON",
contentType: "application/json; charset=utf-8",
data: {jsonStr : jsonStr,path :path },
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
 
Share this answer
 
Comments
TheSniper105 3-Feb-15 20:43pm    
the code work after i had to write another JSON.stringify(jsonStr) beside your code

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900