Answered by:
how to retrieve json or xml response from wcf rest service

Question
-
User-336392218 posted
Hi,
I have a wcf rest service and I'd like to receive data in json or xml based upon contet type of request.
this is the web.config file of the wcf service:
<system.serviceModel> <behaviors> <serviceBehaviors> <behavior> <!-- To avoid disclosing metadata information, set the values below to false before deployment --> <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/> <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information --> <serviceDebug includeExceptionDetailInFaults="false"/> </behavior> </serviceBehaviors> </behaviors> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> <standardEndpoints> <webHttpEndpoint> <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true" defaultOutgoingResponseFormat="Json" crossDomainScriptAccessEnabled="true"></standardEndpoint> </webHttpEndpoint> </standardEndpoints> </system.serviceModel>
I've tested the service with a console app and it works fine:
when I set req.ContentType = "text/json" the result is json.
when I set req.ContentType = "text/xml" the result is xml
In asp.net script code using jquery I cannot obtain the same behaviour:
$(document).ready(function () { $.ajax({ cache: false, url: "http://localhost:28660/RestService", type: "GET", contentType: "application/json; charset=utf-8", dataType: "jsonp", processData: false, error: function (xhr) { console.log(xhr.responseText); }, success: function (result) { console.log(result); } }); });
using the code above I would like to read data in json but I always receive xml formatted data.
need help. thank you.
Tuesday, March 12, 2013 5:55 AM
Answers
-
User-1622227787 posted
Hi, please try remove contentType etc information:
$.ajax({ url: 'http://localhost:8732/Service1/data/10', dataType: 'jsonp', error: function(req, status, ex) { // your code here }, ...
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, March 15, 2013 9:05 AM
All replies
-
User1401801381 posted
hi
you can use
$.ajax({ type: "GET", dataType: "json", contentType: "application/json; charset=utf-8",
or$.getJSON(
Tuesday, March 12, 2013 6:09 AM -
User-336392218 posted
I use
dataType: "jsonp"
because I need it to be cross domain.
I also tried with
contentType: "application/json; charset=utf-8"
but the response from the server is alway xml.
Tuesday, March 12, 2013 6:15 AM -
User-1622227787 posted
Hi, please try remove contentType etc information:
$.ajax({ url: 'http://localhost:8732/Service1/data/10', dataType: 'jsonp', error: function(req, status, ex) { // your code here }, ...
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, March 15, 2013 9:05 AM -
User-1070902567 posted
I am sendingrequest like this https://api.subtledata.com/v1/locations?api_key=SlcrNFg0
response (Output)like this
[{"revenue_centers": [{"default_center": true, "revenue_center_id": 1909, "name": "Unassigned Revenue"}], "receipt_number_instructions": null, "employee_request_through_app": false, "menu_ordering_available": true, "payment_via_credit_card_available_message": null, "postal_code": "78701", "user_rating": "0", "location_id": 918, "app_specials": false, "city": "Austin", "location_name": "notous Test Dinerware System", "tender_types": [], "process_new_credit_cards": false, "table_number_instructions": null, "state": "TX", "color_theme": null, "latitude": 30.26652, "logo_url": "http://www.subtledata.com/I/Logo/?918", "website_url": null, "cross_streets": null, "ordering_available_message": null, "phone": "8143628149", "terminals": [], "location_picture_url": "http://www.subtledata.com/I/Results/?918", "favorites_ordering_available": true, "neighborhood_name": null, "discount_types": [{"default_discount": false, "discount_type_id": 939, "name": "Employee"}, {"default_discount": false, "discount_type_id": 940, "name": "Two Dollar Tuesday"}], "longitude": -97.7429367, "price_rating": 0, "process_pre_authed_cards": false, "address_line_2": null, "address_line_1": "401 Congress Avenue"}]
Using ASP.net
I am doing like this how can do using .NET
please help me
Wednesday, May 1, 2013 8:33 AM