Wednesday, 15 July 2015

Translate PHP cURL to JavaScript (connect to WCF service) -



Translate PHP cURL to JavaScript (connect to WCF service) -

i have wcf api have connect to. don't have access code, works:

$user = "login"; $password = "pwdstr"; $service_credentials = "external_api_user:sometext"; $arg = $user.'|'.sha1($user."somestr".$password); $url='https://mysteryurl.svc/projectsapi/' . $arg; // curl resource $curl = curl_init(); // set options - passing in useragent here curl_setopt_array($curl, array( curlopt_returntransfer => 1, curlopt_url => $url, curlopt_useragent => 'codular sample curl request', curlopt_httpheader => array("authorization: " . base64_encode("basic " + service_credentials)))); // send request & save response $resp $resp = curl_exec($curl); // close request clear resources curl_close($curl); echo $resp;

and webpage info same api. far, i've managed far:

function requestfromapi() { var user = "login"; var password = "pwdstr"; var service_credentials = "external_api_user:sometext"; var arg = user + '|' + sha1(user + "somestr" + password); var url = 'https://mysteryurl.svc/projectsapi/' + arg; $.ajax({ url: url, beforesend: function(xhr) { xhr.setrequestheader("authorization", base64_encode("basic " + service_credentials)); xhr.setrequestheader("user-agent",'codular sample curl request'); }, type: 'get', datatype: 'json', contenttype: 'application/json', success: function (data) { alert(json.stringify(data)); }, error: function(){ alert("cannot data"); } }); };

which of course of study not work. im totally new web development, help appreciated.

chrome console throws this, when loading page:

refused set unsafe header "user-agent" xmlhttprequest cannot load http s://verylongurlasinthecodeabove. no 'access-control-allow-origin' header nowadays on requested resource. origin 'null' hence not allowed access. response had http status code 400.

how can set header? seems me @ to the lowest degree 1 of problem trying access different domain, how that?

is there way access api using javascript? making dumb php 'api' resend info work? im open propositions. in advance!

edit: used jsonp:

<head> <script type="text/javascript" src="js/jquery-2.1.1.js"></script> <script type="text/javascript" src="js/jquery.mobile-1.4.5/jquery.mobile-1.4.5.js"></script> <script type="text/javascript" src="js/functions.js"></script> <script> alert('1'); $(document).ready(function requestfromapi() { var user = "login"; var password = "pwdstr"; var service_credentials = "external_api_user:sometext"; var arg = user + '|' + sha1(user + "somestr" + password); var url='https://mysteryurl.svc/projectsapi/' + arg; $.ajax({ url: url+"?callback=?", beforesend: function(xhr) { xhr.setrequestheader("authorization", base64_encode("basic " + service_credentials) ); xhr.setrequestheader("user-agent",'codular sample curl request'); }, type: 'get', datatype: 'json', contenttype: 'application/json', success: function (datawegotviajsonp) { alert(datawegotviajsonp); }, error: function(){ alert("cannot data"); } }); }); alert('2'); </script> </head>

and bad request error. error might be?

you're on path, error because wpf server doesn't have cors headers enabled, cors headers tell webbrowsers domains allowed connect server, if current domain isn't allowed browser refuses connection.

for illustration on how enable cors on wpf: http://enable-cors.org/server_wcf.html.

javascript php wcf curl

No comments:

Post a Comment