Returning JSON objects from ASP.NET WebMethod without using the response method

My webmethod is quite simple:

<WebMethod(Description:="Does something.")> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
Public Shared Function ReturnJSONData() As Person
    Dim guy As New Person
    guy.Name = "Joe"
    guy.Age = 8
    Return guy
End Function

Below is where I am making an ajax call to the method:

 function GetPerson() {
     PageMethods.ReturnJSONData(OnWSRequestComplete1);
 }
 function OnWSRequestComplete1(result) {
     alert(result.d);
 }

Although, in tools like firebug, I can clearly see the JSON data:

{"d":{"__type":"Person","Name":"Joe","Age":8}}

But, when I try to display the value by using "alert(result.d)", it shows as undefined. Can anyone identify what might be missing here?

Answer №1

After receiving a response from a WebMethod, it is crucial to evaluate the response since it comes back as a string. However, caution should be exercised when considering the use of eval due to potential security vulnerabilities.

For those utilizing jQuery, the recommended approach is to utilize jQuery.parseJSON(result) in order to obtain the desired Javascript object.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Utilize ajax requests to dynamically enable or disable input fields

Currently, I am developing an attendance management system for employees. The attachment below contains three input fields - one dropdown and two calendar fields. The dropdown includes the names of employees, the second field displays the current date and ...

Selecting items in an HTML table using a drag-and-drop feature for selecting square or rectangular sections with colspan and rowspan

I am currently working on an implementation for selecting cells in a table. The challenge I am facing is that the cells in question can have a colspan or rowspan, which means that the selection is not restricted to a square or rectangular shape. For exampl ...

Encountering a 'scheduler flush execution error' and an Uncaught TypeError due to VueJS and Axios integration

After carefully examining my code, I have pinpointed the cause of this error to the following line: treeNodes.value = documentStore.convertToTree((await axios.get('/File')).data); Initially, I receive a warning: Vue warn]: Unhandled error during ...

Executing various database inserts using Ajax with a changing quantity of input fields

I am currently working on adding multiple lines of text fields to my database. <html> <head> </head> <body> <table> <tr> <td><input type="text" id="n ...

The data from the server is inaccessible to node.js

I am a beginner in nodejs and jquery, trying to retrieve data from a server and use it to update a webpage: Using jquery on the client side: I would like to change the text inside '#info' element with the data fetched from the server. $('# ...

Initiate the IE driver in WebDriver NodeJS with the option to disregard protected mode settings and possibly introduce flakiness

I'm attempting to create a driver session using IE capabilities to bypass protected mode settings in Internet Explorer, but I'm unsure of the correct syntax. I've experimented with the following: var driver = new webdriver.Builder().wi ...

Remove an item from a complex JSON structure based on the specified name. The function will then return the

Hey there, I'm just starting out with react native and I have an array of objects. My goal is to remove an inner object from this JSON data. [ { Key: 1, exchnageArr: [ { name: ”FX” }, { name: ”MK” ...

Optimize and compress your Angular 4 code

Currently, I am working with the Asp Core +Angular 4 template and using webpack in Visual Studio 2017. After publishing my app, when I check the content of ClientApp/dist/main-server.js, I notice that it is not minified or uglified. It looks something like ...

Am I using the correct method to parseInt within an if-else statement for Datatable?

Just started working with JS BS Datatables, wondering if this is the correct approach for using parseInt in an if else statement? $(document).ready(function() { $('#myTable').DataTable({ "order": [], "columnDefs ...

Is there a way to remove createdAt and updatedAt from every query in Sequelize?

Currently, I am utilizing Node 14 and Sequelize to model an existing Postgres database. The tables that have already been created do not contain the createdAt or updatedAt columns, so my intention is to set up Sequelize in a way that it will never attempt ...

How to access variables with dynamic names in Vue.js

I'm curious if it's possible to dynamically access variables from Vue’s data collection by specifying the variable name through another variable. For instance, consider the following example: Here are some of the variables/properties: var sit ...

Tips on saving php variable content in HTML "id"

Three variables are used in PHP: message_id, message_title, and message_content. Their content is stored inside HTML 'id' for later use with jQuery. Example: Variables: $id_variable = $rows['id_mensagem']; $message_title_edit = $rows ...

What methods can I use to integrate a Google HeatMap into the GoogleMap object in the Angular AGM library?

I am trying to fetch the googleMap object in agm and utilize it to create a HeatMapLayer in my project. However, the following code is not functioning as expected: declare var google: any; @Directive({ selector: 'my-comp', }) export class MyC ...

Utilizing 'document.execCommand' for handling 'delete' key events in AngularJS

Here is a demo plunkr link that I have created to demonstrate the issue. I am looking to implement the strikeThrough command whenever there is a delete operation. For instance: If the user selects "Text" and presses the delete or backspace key, it should ...

Guide to organizing elements in an array within a separate array

Our array consists of various items: const array = [object1, object2, ...] The structure of each item is defined as follows: type Item = { id: number; title: string contact: { id: number; name: string; }; project: { id: number; n ...

Apps created with PhoneGap will maintain the original sizing of web pages and not automatically resize for display on Android devices

After successfully porting my web-based tool to Android using PhoneGap from my Github repository, I encountered an issue with the display on Android devices. The app loads up too big for the screen, forcing me to constantly scroll around to see and access ...

Saving HTML data into the WordPress database with the help of Ajax

After developing a WordPress plugin, I encountered an issue while working with div elements. My goal is to retrieve the content of a specific div using the following code snippet: var archive = j( "div.page2" ).html(); console.log(archive); Everything wo ...

What could be causing my directive to not display my scope?

I am currently developing a directive that includes a custom controller, and I am testing the scope functionality. However, I am facing an issue where the ng-show directive is not functioning as expected when trying to test if the directive has a scope fro ...

Turning On/Off Input According to Selection (Using jQuery)

<select name="region" class="selection" id="region"> <option value="choice">Choice</option> <option value="another">Another</option> </select> <input type="text" name="territory" class="textfield" id="territo ...

Converting a string with array-like data into an ArrayList in Java

Seeking to transform a String like this: "[1,2,3]" into an ArrayList in Java has been my current challenge. My approach so far has been utilizing the GSon library to convert the mentioned String to a List using: Gson - convert from Json to a typed Array ...