Web service data format identification

Asked

Viewed 29 times

1

Good night! I am implemented a service in one system for other systems to consume. This system consumes data in XML and JSON formats. I want to know if there is any way to identify when the user sends a string in XML format. Here is my code.

        $(function() {
        $.ajax({
            type: "post",
            contentType: "application/xml; charset=utf-8",
            url: 'http://www.e-sms.com.br/api',
            data: { xml : "<?xml version='1.0' encoding='UTF-8'?> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note>"},
            dataType: "json",
            success: function(data){
                console.log(data);
            },
            error: function (request){
                console.log(request);
            }
        });        
    });

This code,works when the Contenttype property is removed. So far, I’m believing Contenttype is the secret. My Server has the following header.

header('Access-Control-Allow-Headers: *');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, PUT');
header('Content-Type: text/html; charset=utf-8');
header('Content-Type: text/json; charset=utf-8');

header, on the server side uses the default Content-Type. I believe that by setting this attribute in my Ajax request, I would be soiling the server that uses it. But I can’t seem to make the deal work. I wonder if anyone can help me ?

  • Using Contenttype is wrong in this case. Note that you are not sending an XML content, but an object where there is an "xml" name variable that in turn has a text string, this text being an xml. About the headlines Content-Type: text/html; charset=utf-8 and Content-Type: text/json; charset=utf-8 no use putting the 2, because the second replaces the first. Make a condition if to decide which php to consider.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.