1

I wanted to perform a POST request to an external url using javascript. Currently I use php server side to sent the request

$data = array(
        'TokenExID' => $tokenex_id,
        'APIKey' => $api_key,
        'EcryptedData' => $encrypted_data,
        'TokenScheme' => 4
    );
    $ch = curl_init("https://api.testone.com/TokenServices.svc/REST/TokenizeFromEncryptedValue");

        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                                        'Content-Type: application/json', //                  
                                        'Accept: application/json')       //
                                        );
           curl_setopt($ch, CURLOPT_POST, 1);
           curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
           curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
           curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

The same code I wanted to do it in a javascript.

var url = "https://api.testone.com/TokenServices.svc/REST/TokenizeFromEncryptedValue";

var params = "abcd";
http.open("POST", url, true);

//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");

http.onreadystatechange = function() {//Call a function when the state changes.
    if(http.readyState == 4 && http.status == 200) {
        alert(http.responseText);
    }
}

http.send(params);

I got this sample code of javascript,but im not sure how to do this.Im new to javascript but couldnt find a way to do it.Can some one help me out?

Dairo
  • 804
  • 1
  • 9
  • 21
Piya
  • 1,084
  • 3
  • 20
  • 41
  • possible duplicate: http://stackoverflow.com/questions/8567114/how-to-make-an-ajax-call-without-jquery – ilpaijin Mar 29 '14 at 11:41
  • What happens when you run that code? What does the JavaScript console say? What does the Net tab of your browser's developer tools say? – Quentin Mar 30 '14 at 20:56
  • possible duplicate of [Ajax request to external url using javascript](http://stackoverflow.com/questions/22730894/ajax-request-to-external-url-using-javascript) – Quentin Mar 30 '14 at 20:56
  • You appear to have asked about **seven** variations of this question over the last couple of days. Most of them don't have accepted answers. If you aren't getting suitable answers to your questions, then edit them to add more information, don't ask variations of them. – Quentin Mar 30 '14 at 20:58

1 Answers1

3

if you are new to javascript I strongly recommend that you use jQuery, it will ease your work a lot. Search online how to include it in your page, then this is the code.

var data = {
    param1: "value1",
    param2: "value2"
    // add here more params id needed followinf the same model

};

$.ajax({
    type: "POST",
    url: 'http://www.myrestserver.com/api',
    data: data,
    success: success
});

function success(result) {
    // do something here if the call is successful
    alert(result);
}

https://api.jquery.com/jQuery.ajax/

If you want to go further check this: JavaScript post request like a form submit

Hope it helps, Paul

Community
  • 1
  • 1
Paul
  • 6,867
  • 5
  • 28
  • 36
  • Thanks Paul.Ill test this,also to ask,is it possible to perform the same using javascript? – Piya Mar 29 '14 at 11:51
  • @PiyaSharma Jquery is the framework of javascript and it is good to use the jquery-ajax instead of javascript.so you should go with paul's answer. – Harshal Mahajan Mar 29 '14 at 12:09
  • Thanks,I tried with this,couldnt succeed :( I have the fiddle here http://jsfiddle.net/P8Afm/ – Piya Mar 29 '14 at 12:10
  • you have not include the jquery.js. – Harshal Mahajan Mar 29 '14 at 12:18
  • Hi, your code has a couple of errors. You cannot copypaste php code into js. I updated my example. Please note the syntax of data which is a javascript object. Do not use "=>" like in php arrays. If you have loaded jQuery and you copy this code and put your real parameters it should work. – Paul Mar 30 '14 at 20:52
  • What was the problem with the original code that this fixes (if it does fix it). You don't seem to have identified the actual problem. – Quentin Mar 30 '14 at 20:55
  • I did. Javascript does not accept "=>" syntax so the code is not event interpreted. (Actually this is a reply to the JsFiddle example, not to my previous code which was already working). Let me know if this explanation is clear. – Paul Mar 30 '14 at 21:06