0

Wrote a webmethod in ASMX to return JSON data to Angular application. So here is how the Webmethod looks like

    [WebMethod]        
    [ScriptMethod(UseHttpGet =true,ResponseFormat =ResponseFormat.Json)]
    public MyProjects getReport(int reportId)
    {
    }

Here is how I'm calling it from Angular

this.client.get('http://localhost:51071/webservice1.asmx/getReport?reportId=596', 
{ withCredentials: true, responseType: 'text' })
   .subscribe( (data) => console.log(data));

But the problem is the data received at angular app is in XML format. Thats the reason I specified text there. If I specify json there it will ofcourse trigger error saying invalid json data.

I have absolutely no idea what I did wrong or what to change to get pure JSON

Here is how the console output looks like

<?xml version="1.0" encoding="utf-8"?>
<Projects xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
  <Success>true</Success>
  <TotalRecords>391</TotalRecords>
  <Data>
    <ProjectData>
      <ProjectCode>E6012</ProjectCode>
Sandeep Thomas
  • 4,303
  • 14
  • 61
  • 132
  • Does this answer your question? [asp.net asmx web service returning xml instead of json](https://stackoverflow.com/questions/11088294/asp-net-asmx-web-service-returning-xml-instead-of-json) – ruth Mar 30 '20 at 09:14
  • Sorry... It doest. I have already tried that.. – Sandeep Thomas Mar 30 '20 at 09:21

1 Answers1

0

Are you sure about your IIS configuration?

Open IIS Manager Display properties for the IIS Server Click MIME Types and then find a row with the JSON extension: File name extension: .json MIME type: application/json

if not found please create it.

Go back to the properties for IIS Server Click on Handler Mappings Add a script map Request path: *.json Executable: C:\WINDOWS\system32\inetsrv\asp.dll Name: JSON

i hope that can help you

  • Mine is iis express application, not iis. Is there is any specific conf for iis epress? – Sandeep Thomas Mar 30 '20 at 09:50
  • Open Visual Studio Command Prompt, using Run as Administrator Option. Navigate to IIS Express Directory (C:\Program Files (x86)\IIS Express) Run the following command: appcmd set config /section:staticContent /+[fileExtension=’JSON’,mimeType=’application/x-javascript’] – amine_mouhib Mar 30 '20 at 11:53