Ayush Patil

Ayush Patil

  • NA
  • 178
  • 20.5k

failed to return json to ajax give error [object object]

Sep 2 2018 1:23 PM
 I tried to get result but failed. I dont understand what is the issue.
from my scrip ActionResult calling that OK but whene debbeger goes to return json 
its giving me error alert as [object object] and it not going to success in script. Plz help me out from this issue.
thank you. 
 
 
Here Is my controller code:
 
public ActionResult CreateQuotation(Int objInt)
{
string json = string.Empty;
Uri uri = null;
try
{
string errorText = string.Empty;
string errorCode = string.Empty;
string outputXML = string.Empty;
double netPremium = 0;
double serviceTax = 0;
double totalPremium = 0;
string proposalNumber = string.Empty;
string quotationNumber = string.Empty;
string ResponseTime = string.Empty;
var result = "";
string authKey = GetAuthenticationKey(objInt.Type);
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
AuthenticationKey obj = JsonConvert.DeserializeObject(authKey);
string authenticationKey = obj.GetAuthenticationKeyResult.AuthKey;
XmlDocument InputXML = new XmlDocument();
InputXML.Load(@"D:\XMLInfo\it\Request.xml");
string xmlcontents = InputXML.InnerXml;
string inputxml = xmlcontents.ToString();
string ProductCode = objIntegration.ProductCode;
if (objInt.Type == "AAA")
{
json = @"{
""Input"":
{
""UserID"": ""AAA"",
""Password"": ""password"",
""UserRole"": ""ADMIN"",
""Code"": ""1"",
""Mode"": ""NEW"",
""InputXML"": ""1"",
}
}";
uri = new Uri("https://LOCALHOST");
}
else
{
json = @"{
""Input"":
{
""UserID"": ""tes"",
""Password"": ""password1"",
""UserRole"": ""ADMIN"",
""Code"": ""1"",
""InputXML"": ""1"",
}
}";
dynamic rss = JObject.Parse(json);
JObject tempUserData = (JObject)rss.SelectToken("Input");
tempUserData["Code"] = ProductCode;
tempUserData["InputXML"] = inputxml;
string data1 = rss.ToString();
if (uri.Scheme == Uri.UriSchemeHttps)
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
request.Method = WebRequestMethods.Http.Post;
//request.Method = "POST";
request.KeepAlive = false;
request.ContentLength = data1.Length;
request.ContentType = "application/json; charset=utf-8";
//request.ContentType = "application/json";
//request.ContentType = "application/x-www-form-urlencoded";
StreamWriter writer = new StreamWriter(request.GetRequestStream());
writer.Write(data1);
writer.Close();
string startTime = DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string tmp = reader.ReadToEnd();
string endTime = DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss");
//DateTime startDate = Convert.ToDateTime(startTime);
//DateTime endate = Convert.ToDateTime(endTime);
DateTime startDate = DateTime.ParseExact(startTime, "MM/dd/yyyy HH:mm:ss", null);
DateTime endate = DateTime.ParseExact(endTime, "MM/dd/yyyy HH:mm:ss", null);
TimeSpan ts = endate.Subtract(startDate);
int second = (int)ts.Seconds;
ResponseTime = second.ToString();
Response.Write(second.ToString());
XMLResponse objXMLResponse = JsonConvert.DeserializeObject(tmp);
errorText = objXMLResponse.Result.ErrorText;
errorCode = objXMLResponse.Result.ErrorCode;
outputXML = objXMLResponse.Result.OutputXML;
netPremium = objXMLResponse.Result.NetPremium;
serviceTax = objXMLResponse.Result.ServiceTax;
total = objXMLResponse.Result.TotalPremium;
}
}
catch (Exception ex)
{
}
return Json(new
{
jsErrorMsg = errorText,
jsResponseXML = outputXML,
jstotal = total
jsResponseTime = ResponseTime
}, JsonRequestBehavior.AllowGet);
}
 
Here is my script:
 
function CreateQoute() {
var objIntegration = {
ProductCode: $('#ProductCode').val(),
environmentType: ($("input[type=radio]:checked").val()),
};
$.ajax({
type: "POST",
data: JSON.stringify(objIntegration),
url: "/Home/CreateQuotation",
contentType: "application/json;charset=utf-8",
dataType: "Json",
success: function (data) {  // Inside script not loading.
if (data.jsError = "") {
alert('Hi');
}
else
{
alert('request failed');
}
},
error: function (data) {
alert(data);
}
});
}