-2

Here is my URL Please suggest me how can I get values in this URL , it's form a XML API of SMS gateway now I have to execute it and send SMS on given numbers in URL please suggest me I have tried everything.

http://example.com/api/postsms.php?data=%3CMESSAGE%3E%0A%3CAUTHKEY%3EYour%20auth%20key%3C/AUTHKEY%3E%0A%3CSENDER%3ESenderID%3C/SENDER%3E%0A%3CROUTE%3ETemplate%3C/ROUTE%3E%0A%3CCAMPAIGN%3Ecampaign%20name%3C/CAMPAIGN%3E%0A%3CCOUNTRY%3Ecountry%20code%3C/COUNTRY%3E%0A%3CSMS%20TEXT%3D%22message1%22%3E%0A%3CADDRESS%20TO%3D%22number1%22%3E%3C/ADDRESS%3E%0A%3C/SMS%3E%0A%3C/MESSAGE%3E%0A
  • 3
    Exact duplicate with a very good solution here: http://stackoverflow.com/questions/19491336/get-url-parameter-jquery-or-how-to-get-query-string-values-in-js – Cobus Kruger Jun 09 '16 at 08:16

1 Answers1

3

In javascript, you can just use decodeUri

var xml = decodeURI('%3CMESSAGE%3E%0A%3CAUTHKEY%3EYour%20auth%20key%3C/AUTHKEY%3E%0A%3CSENDER%3ESenderID%3C/SENDER%3E%0A%3CROUTE%3ETemplate%3C/ROUTE%3E%0A%3CCAMPAIGN%3Ecampaign%20name%3C/CAMPAIGN%3E%0A%3CCOUNTRY%3Ecountry%20code%3C/COUNTRY%3E%0A%3CSMS%20TEXT%3D%22message1%22%3E%0A%3CADDRESS%20TO%3D%22number1%22%3E%3C/ADDRESS%3E%0A%3C/SMS%3E%0A%3C/MESSAGE%3E%0A');

gives you

<MESSAGE>
    <AUTHKEY>Your auth key</AUTHKEY>
    <SENDER>SenderID</SENDER>
    <ROUTE>Template</ROUTE>
    <CAMPAIGN>campaign name</CAMPAIGN>
    <COUNTRY>country code</COUNTRY>
    <SMS TEXT%3D"message1">
       <ADDRESS TO%3D"number1"></ADDRESS>
   </SMS>
</MESSAGE>

As you can see, the text is encoded twice.
You will need to decode again.

var finalResult = decodeUriComponent(xml) // gives you

<MESSAGE>
    <AUTHKEY>Your auth key</AUTHKEY>
    <SENDER>SenderID</SENDER>
    <ROUTE>Template</ROUTE>
    <CAMPAIGN>campaign name</CAMPAIGN>
    <COUNTRY>country code</COUNTRY>
    <SMS TEXT="message1">
        <ADDRESS TO="number1"></ADDRESS>
    </SMS>
</MESSAGE>

Which is now valid xml, and can be parsed using jQuery.

$.parseXML(finalResult);

additionally

// to parse the complete url in chrome and mozilla
var xmlString = new URLSearchParams('YOUR FULL URL HERE').values().next()
Jim
  • 14,952
  • 15
  • 80
  • 167
  • I am not able to see xml result I only get this "http://example.com/api/postsms.php?data= Your auth key SenderID Template campaign name country code " – Arpit Pathak Jun 09 '16 at 09:14
  • @arpit, you will also need to extract the data component of the url. javascript has no native method to do this. http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript I did it manually. if you don't care about IE you can try this: https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams – Jim Jun 09 '16 at 09:20