Ajax Json undefined object on success
I am trying to use an API request from a widget I have installed but I have noticed that my Json data returns a "Object reference not set to instance of an object" exception on success.
Here is the Json:
{
"ApiKey": "d4f37ba05a1941928de3109c8632b1ce",
"Text": "tesst%0A",
"Language": "en",
"Rule": "Preediting_Forum",
"Grammar": "1",
"Spell": "1",
"Style": "1",
"SessionMetadata": "Preediting_Forum;0",
"RequestFormat": "HTML"
}
Here is the ajax code for the post request:
$.ajax({
url: settings.AcceptServerPath + "/Core/AcceptRequest",
dataType: "json",
contentType: "application/json",
type: "POST",
async: true,
cache: true,
data: jsonData,
success: function (data) {
console.log(data);
handleRequestStatus(data, "ACCEPT");
},
complete: function (data) {},
error: function (error) {
displayFailedMessage("ACCEPT", configuration.language.errorRequestFailed);
}
});
What could be the cause?
Edit
Here is the C# code:
[HttpPost]
public ActionResult AcceptRequest(CoreApiRequest requestObject)
{
try
{
if (requestObject == null || requestObject.Text.Length == 0)
requestObject = (CoreApiRequest)(new DataContractJsonSerializer(requestObject.GetType())).ReadObject(Request.InputStream);
Dictionary<string, string> parameters = new Dictionary<string, string>();
if (requestObject.ApiKey != null && requestObject.ApiKey.Length > 0)
{
parameters.Add("apiKey", requestObject.ApiKey);
}
else
{
parameters.Add("username", requestObject.User);
parameters.Add("password", requestObject.Password);
}
string text = HttpUtility.UrlDecode(requestObject.Text, System.Text.Encoding.UTF8);
parameters.Add("text", text);
parameters.Add("languageid", requestObject.Language);
parameters.Add("ruleset", requestObject.Rule);
parameters.Add("grammar", requestObject.Grammar);
parameters.Add("spell", requestObject.Spell);
parameters.Add("style", requestObject.Style);
parameters.Add("requestFormat", requestObject.RequestFormat);
if (requestObject.GlobalSessionId != null && requestObject.GlobalSessionId.Length > 0)
{
parameters.Add("globalSessionId", requestObject.GlobalSessionId);
}
if (requestObject.IEDomain != null && requestObject.IEDomain.Length > 0)
parameters.Add("ieDomain", requestObject.IEDomain);
if (requestObject.SessionMetadata != null && requestObject.SessionMetadata.Length > 0)
parameters.Add("sessionMetadata", requestObject.SessionMetadata);
var model = AcceptCoreManager.GenericRequest(parameters);
return Json(model);
}
catch (Exception e)
{
var errorModel = new CoreApiException(e.Message, "Accept Request Controller");
return Json(errorModel);
}
}
There is also a [RestHttpVerbFilter] for the AcceptRequest. Not sure which one is used in this case...
c# jquery json ajax
add a comment |
I am trying to use an API request from a widget I have installed but I have noticed that my Json data returns a "Object reference not set to instance of an object" exception on success.
Here is the Json:
{
"ApiKey": "d4f37ba05a1941928de3109c8632b1ce",
"Text": "tesst%0A",
"Language": "en",
"Rule": "Preediting_Forum",
"Grammar": "1",
"Spell": "1",
"Style": "1",
"SessionMetadata": "Preediting_Forum;0",
"RequestFormat": "HTML"
}
Here is the ajax code for the post request:
$.ajax({
url: settings.AcceptServerPath + "/Core/AcceptRequest",
dataType: "json",
contentType: "application/json",
type: "POST",
async: true,
cache: true,
data: jsonData,
success: function (data) {
console.log(data);
handleRequestStatus(data, "ACCEPT");
},
complete: function (data) {},
error: function (error) {
displayFailedMessage("ACCEPT", configuration.language.errorRequestFailed);
}
});
What could be the cause?
Edit
Here is the C# code:
[HttpPost]
public ActionResult AcceptRequest(CoreApiRequest requestObject)
{
try
{
if (requestObject == null || requestObject.Text.Length == 0)
requestObject = (CoreApiRequest)(new DataContractJsonSerializer(requestObject.GetType())).ReadObject(Request.InputStream);
Dictionary<string, string> parameters = new Dictionary<string, string>();
if (requestObject.ApiKey != null && requestObject.ApiKey.Length > 0)
{
parameters.Add("apiKey", requestObject.ApiKey);
}
else
{
parameters.Add("username", requestObject.User);
parameters.Add("password", requestObject.Password);
}
string text = HttpUtility.UrlDecode(requestObject.Text, System.Text.Encoding.UTF8);
parameters.Add("text", text);
parameters.Add("languageid", requestObject.Language);
parameters.Add("ruleset", requestObject.Rule);
parameters.Add("grammar", requestObject.Grammar);
parameters.Add("spell", requestObject.Spell);
parameters.Add("style", requestObject.Style);
parameters.Add("requestFormat", requestObject.RequestFormat);
if (requestObject.GlobalSessionId != null && requestObject.GlobalSessionId.Length > 0)
{
parameters.Add("globalSessionId", requestObject.GlobalSessionId);
}
if (requestObject.IEDomain != null && requestObject.IEDomain.Length > 0)
parameters.Add("ieDomain", requestObject.IEDomain);
if (requestObject.SessionMetadata != null && requestObject.SessionMetadata.Length > 0)
parameters.Add("sessionMetadata", requestObject.SessionMetadata);
var model = AcceptCoreManager.GenericRequest(parameters);
return Json(model);
}
catch (Exception e)
{
var errorModel = new CoreApiException(e.Message, "Accept Request Controller");
return Json(errorModel);
}
}
There is also a [RestHttpVerbFilter] for the AcceptRequest. Not sure which one is used in this case...
c# jquery json ajax
2
That comes from your C# code. You need to debug it.
– SLaks
Nov 21 '18 at 16:04
@SLaks I edit my post with the C# code. I'm not very familiar with it so how should I proceed to debug it in this case?
– Simon
Nov 22 '18 at 9:09
Learn how to use Visual Studio's debugger. Also, delete yourcatch
block so that the request will actually fail instead of silently returning useless JSON.
– SLaks
Nov 25 '18 at 16:36
add a comment |
I am trying to use an API request from a widget I have installed but I have noticed that my Json data returns a "Object reference not set to instance of an object" exception on success.
Here is the Json:
{
"ApiKey": "d4f37ba05a1941928de3109c8632b1ce",
"Text": "tesst%0A",
"Language": "en",
"Rule": "Preediting_Forum",
"Grammar": "1",
"Spell": "1",
"Style": "1",
"SessionMetadata": "Preediting_Forum;0",
"RequestFormat": "HTML"
}
Here is the ajax code for the post request:
$.ajax({
url: settings.AcceptServerPath + "/Core/AcceptRequest",
dataType: "json",
contentType: "application/json",
type: "POST",
async: true,
cache: true,
data: jsonData,
success: function (data) {
console.log(data);
handleRequestStatus(data, "ACCEPT");
},
complete: function (data) {},
error: function (error) {
displayFailedMessage("ACCEPT", configuration.language.errorRequestFailed);
}
});
What could be the cause?
Edit
Here is the C# code:
[HttpPost]
public ActionResult AcceptRequest(CoreApiRequest requestObject)
{
try
{
if (requestObject == null || requestObject.Text.Length == 0)
requestObject = (CoreApiRequest)(new DataContractJsonSerializer(requestObject.GetType())).ReadObject(Request.InputStream);
Dictionary<string, string> parameters = new Dictionary<string, string>();
if (requestObject.ApiKey != null && requestObject.ApiKey.Length > 0)
{
parameters.Add("apiKey", requestObject.ApiKey);
}
else
{
parameters.Add("username", requestObject.User);
parameters.Add("password", requestObject.Password);
}
string text = HttpUtility.UrlDecode(requestObject.Text, System.Text.Encoding.UTF8);
parameters.Add("text", text);
parameters.Add("languageid", requestObject.Language);
parameters.Add("ruleset", requestObject.Rule);
parameters.Add("grammar", requestObject.Grammar);
parameters.Add("spell", requestObject.Spell);
parameters.Add("style", requestObject.Style);
parameters.Add("requestFormat", requestObject.RequestFormat);
if (requestObject.GlobalSessionId != null && requestObject.GlobalSessionId.Length > 0)
{
parameters.Add("globalSessionId", requestObject.GlobalSessionId);
}
if (requestObject.IEDomain != null && requestObject.IEDomain.Length > 0)
parameters.Add("ieDomain", requestObject.IEDomain);
if (requestObject.SessionMetadata != null && requestObject.SessionMetadata.Length > 0)
parameters.Add("sessionMetadata", requestObject.SessionMetadata);
var model = AcceptCoreManager.GenericRequest(parameters);
return Json(model);
}
catch (Exception e)
{
var errorModel = new CoreApiException(e.Message, "Accept Request Controller");
return Json(errorModel);
}
}
There is also a [RestHttpVerbFilter] for the AcceptRequest. Not sure which one is used in this case...
c# jquery json ajax
I am trying to use an API request from a widget I have installed but I have noticed that my Json data returns a "Object reference not set to instance of an object" exception on success.
Here is the Json:
{
"ApiKey": "d4f37ba05a1941928de3109c8632b1ce",
"Text": "tesst%0A",
"Language": "en",
"Rule": "Preediting_Forum",
"Grammar": "1",
"Spell": "1",
"Style": "1",
"SessionMetadata": "Preediting_Forum;0",
"RequestFormat": "HTML"
}
Here is the ajax code for the post request:
$.ajax({
url: settings.AcceptServerPath + "/Core/AcceptRequest",
dataType: "json",
contentType: "application/json",
type: "POST",
async: true,
cache: true,
data: jsonData,
success: function (data) {
console.log(data);
handleRequestStatus(data, "ACCEPT");
},
complete: function (data) {},
error: function (error) {
displayFailedMessage("ACCEPT", configuration.language.errorRequestFailed);
}
});
What could be the cause?
Edit
Here is the C# code:
[HttpPost]
public ActionResult AcceptRequest(CoreApiRequest requestObject)
{
try
{
if (requestObject == null || requestObject.Text.Length == 0)
requestObject = (CoreApiRequest)(new DataContractJsonSerializer(requestObject.GetType())).ReadObject(Request.InputStream);
Dictionary<string, string> parameters = new Dictionary<string, string>();
if (requestObject.ApiKey != null && requestObject.ApiKey.Length > 0)
{
parameters.Add("apiKey", requestObject.ApiKey);
}
else
{
parameters.Add("username", requestObject.User);
parameters.Add("password", requestObject.Password);
}
string text = HttpUtility.UrlDecode(requestObject.Text, System.Text.Encoding.UTF8);
parameters.Add("text", text);
parameters.Add("languageid", requestObject.Language);
parameters.Add("ruleset", requestObject.Rule);
parameters.Add("grammar", requestObject.Grammar);
parameters.Add("spell", requestObject.Spell);
parameters.Add("style", requestObject.Style);
parameters.Add("requestFormat", requestObject.RequestFormat);
if (requestObject.GlobalSessionId != null && requestObject.GlobalSessionId.Length > 0)
{
parameters.Add("globalSessionId", requestObject.GlobalSessionId);
}
if (requestObject.IEDomain != null && requestObject.IEDomain.Length > 0)
parameters.Add("ieDomain", requestObject.IEDomain);
if (requestObject.SessionMetadata != null && requestObject.SessionMetadata.Length > 0)
parameters.Add("sessionMetadata", requestObject.SessionMetadata);
var model = AcceptCoreManager.GenericRequest(parameters);
return Json(model);
}
catch (Exception e)
{
var errorModel = new CoreApiException(e.Message, "Accept Request Controller");
return Json(errorModel);
}
}
There is also a [RestHttpVerbFilter] for the AcceptRequest. Not sure which one is used in this case...
c# jquery json ajax
c# jquery json ajax
edited Nov 22 '18 at 9:08
Simon
asked Nov 21 '18 at 16:02
SimonSimon
7716
7716
2
That comes from your C# code. You need to debug it.
– SLaks
Nov 21 '18 at 16:04
@SLaks I edit my post with the C# code. I'm not very familiar with it so how should I proceed to debug it in this case?
– Simon
Nov 22 '18 at 9:09
Learn how to use Visual Studio's debugger. Also, delete yourcatch
block so that the request will actually fail instead of silently returning useless JSON.
– SLaks
Nov 25 '18 at 16:36
add a comment |
2
That comes from your C# code. You need to debug it.
– SLaks
Nov 21 '18 at 16:04
@SLaks I edit my post with the C# code. I'm not very familiar with it so how should I proceed to debug it in this case?
– Simon
Nov 22 '18 at 9:09
Learn how to use Visual Studio's debugger. Also, delete yourcatch
block so that the request will actually fail instead of silently returning useless JSON.
– SLaks
Nov 25 '18 at 16:36
2
2
That comes from your C# code. You need to debug it.
– SLaks
Nov 21 '18 at 16:04
That comes from your C# code. You need to debug it.
– SLaks
Nov 21 '18 at 16:04
@SLaks I edit my post with the C# code. I'm not very familiar with it so how should I proceed to debug it in this case?
– Simon
Nov 22 '18 at 9:09
@SLaks I edit my post with the C# code. I'm not very familiar with it so how should I proceed to debug it in this case?
– Simon
Nov 22 '18 at 9:09
Learn how to use Visual Studio's debugger. Also, delete your
catch
block so that the request will actually fail instead of silently returning useless JSON.– SLaks
Nov 25 '18 at 16:36
Learn how to use Visual Studio's debugger. Also, delete your
catch
block so that the request will actually fail instead of silently returning useless JSON.– SLaks
Nov 25 '18 at 16:36
add a comment |
1 Answer
1
active
oldest
votes
i have the same issue,
its because the response is 'no content',
when the response is 'no content', ie. nothing returned. ajax shows undefined object.
so make sure response contains json object. just try alert(JSON.stringify(data));
Has I said the data returns {"Exception":"Object reference not set to instance of an object","Context":"Accept Request Controller","ResponseStatus":"FAILED"...}. What I wanted to know is what causes this error even though the ajax request succeeded?
– Simon
Nov 22 '18 at 9:05
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53416012%2fajax-json-undefined-object-on-success%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
i have the same issue,
its because the response is 'no content',
when the response is 'no content', ie. nothing returned. ajax shows undefined object.
so make sure response contains json object. just try alert(JSON.stringify(data));
Has I said the data returns {"Exception":"Object reference not set to instance of an object","Context":"Accept Request Controller","ResponseStatus":"FAILED"...}. What I wanted to know is what causes this error even though the ajax request succeeded?
– Simon
Nov 22 '18 at 9:05
add a comment |
i have the same issue,
its because the response is 'no content',
when the response is 'no content', ie. nothing returned. ajax shows undefined object.
so make sure response contains json object. just try alert(JSON.stringify(data));
Has I said the data returns {"Exception":"Object reference not set to instance of an object","Context":"Accept Request Controller","ResponseStatus":"FAILED"...}. What I wanted to know is what causes this error even though the ajax request succeeded?
– Simon
Nov 22 '18 at 9:05
add a comment |
i have the same issue,
its because the response is 'no content',
when the response is 'no content', ie. nothing returned. ajax shows undefined object.
so make sure response contains json object. just try alert(JSON.stringify(data));
i have the same issue,
its because the response is 'no content',
when the response is 'no content', ie. nothing returned. ajax shows undefined object.
so make sure response contains json object. just try alert(JSON.stringify(data));
answered Nov 21 '18 at 16:37
Mahesh ShenoiMahesh Shenoi
11
11
Has I said the data returns {"Exception":"Object reference not set to instance of an object","Context":"Accept Request Controller","ResponseStatus":"FAILED"...}. What I wanted to know is what causes this error even though the ajax request succeeded?
– Simon
Nov 22 '18 at 9:05
add a comment |
Has I said the data returns {"Exception":"Object reference not set to instance of an object","Context":"Accept Request Controller","ResponseStatus":"FAILED"...}. What I wanted to know is what causes this error even though the ajax request succeeded?
– Simon
Nov 22 '18 at 9:05
Has I said the data returns {"Exception":"Object reference not set to instance of an object","Context":"Accept Request Controller","ResponseStatus":"FAILED"...}. What I wanted to know is what causes this error even though the ajax request succeeded?
– Simon
Nov 22 '18 at 9:05
Has I said the data returns {"Exception":"Object reference not set to instance of an object","Context":"Accept Request Controller","ResponseStatus":"FAILED"...}. What I wanted to know is what causes this error even though the ajax request succeeded?
– Simon
Nov 22 '18 at 9:05
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53416012%2fajax-json-undefined-object-on-success%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
That comes from your C# code. You need to debug it.
– SLaks
Nov 21 '18 at 16:04
@SLaks I edit my post with the C# code. I'm not very familiar with it so how should I proceed to debug it in this case?
– Simon
Nov 22 '18 at 9:09
Learn how to use Visual Studio's debugger. Also, delete your
catch
block so that the request will actually fail instead of silently returning useless JSON.– SLaks
Nov 25 '18 at 16:36