简体   繁体   中英

Delphi Datasnap testing with javascript client

Trying to test a new datasnap server with a simple javascript client. It works up until i turn on authorization on the server then i keep getting 401 errors and the onAuthorization event is not passing in any usernames or passwords they are always blank.

$(document).ready(function(e) {

var jsonStr = {
"ContactName": "smith, anthony",
"FirstName": "anthony",
"LastName": "smith"
  };


  function make_base_auth(user, password) {
var tok = user + ':' + password;
var hash = btoa(tok);
return "Basic " + hash;
  }

  var userName = "admin";
  var password = "admin";
  var authStr = convertStringToBase64(userName + ":" + password);
  var authObj = '{"authentication":authStr}';


  $.ajax({
url: "http://127.0.0.1:8086/api/services/Customers/RegisterCustomer",
headers: {
  "Authorization": make_base_auth(userName, password)
},
beforeSend: function(xhr) {
  xhr.setRequestHeader("Content-Type", "application/json");
  xhr.setRequestHeader("Accept", "application/json");
  //xhr.setRequestHeader ("Authorization", make_base_auth (userName, password)); 
},
crossDomain: true,
type: "POST",
dataType: "json",
data: JSON.stringify(jsonStr),

success: function(result, status, jqXHR) {
  $("preerror").text("IT WORKED");
  var jsonStr = result;
  var jsonObj = JSON.parse(jsonStr);
  var jsonPretty = JSON.stringify(jsonObj, null, '\t');

  $("pre").text(jsonPretty);
},
error(jqXHR, textStatus, errorThrown) {
  $("pre").text("FAIL");
}
  });

});

In your quoted source you prohibited (commented out) authorisation part

 //xhr.setRequestHeader ("Authorization", make_base_auth (userName, password)); 

Can you show real HTTP traffic log between client and server?

Actually two logs - between server and working authenticated Delphi client - and between server and failed JS client. Then there would be seen difference....

like https://stackoverflow.com/a/17549592/976391

Did not resolve, spent waaaay too long trying to solve this one. In the end only needed a test client and got a working php client going very quickly. Seemed so close yet never worked not exactly sure but got enough to keep goin thanx anyway

try put this in webmodule

procedure TWebModule1.WebModuleBeforeDispatch(Sender: TObject;
  Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
begin
  Response.SetCustomHeader('access-control-allow-origin','*');
  if FServerFunctionInvokerAction <> nil then
    FServerFunctionInvokerAction.Enabled := AllowServerFunctionInvoker;
end;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM