使用Angularjs将Json数据从一个应用程序发送到在不同端口上运行的另一个应用程序

时间:2019-06-06 11:31:59

标签: json angularjs

我在tomcat中部署了主要的后端和前端Web应用程序,在tomcat中部署了另一个应用程序,即sms网关,但没有前端UI。 可以从https://localhost:443/client-app访问主应用,并且短信网关在http://localhost:9191/message-gateway-0.0.1上运行,但是发送短信的端点是http://localhost:9191/message-gateway-0.0.1/sms。 通过运行在https://localhost:443/client-app上的AngularJs网络应用程序,我希望能够设计一个用于发送短信的简单UI。

提交功能如下:-

scope.submit = function () {
    var messagejson = {};
    messagejson.internalId = '55';
    messagejson.mobileNumber = +9128877477433;
    messagejson.message = 'DEAR IPPEZ, Your ATM Card is read for collection at our Head Office.;
    messagejson.providerId = '2';
    $http({
        method: 'POST',
        url: 'http://localhost:9191/message-gateway-0.0.1/sms',
        data: messagejson,
        headers: {
            "Tenant-App-Key": 'e4600907-f884-4abe-b50b-1c26cc349871'
        }
    }).then(function successCallback(response) {
    if (response.data)
        alert(response.status + ": SMS message sent Successfully!);
    }, function errorCallback(response) {
        alert(response.status + ": Service not Exists);
    });
};

在提交时没有发送消息,并且当我检查网络控制台时,我看到如下错误WebConsole Error Screen 但是从POSTMAN那里获得成功。

我们非常感谢您提供的协助和解决方法。谢谢

Postman Screen

1 个答案:

答案 0 :(得分:0)

您可能会遇到CORS问题。浏览器禁止在不同域( localhost:443 localhost:9191 )之间发送请求。邮递员不是浏览器,因此可以使用。

您有2个选择:

  1. Enable CORS在您的SMS应用中。您必须添加特殊的响应标头(Access-Control-Allow-Origin),然后就可以开始使用了。
  2. 创建后端-后端通信。在这种情况下,您的流程应类似于:客户端应用UI->客户端应用后端->消息网关后端。对我来说,这听起来更好,因为您可以在客户端应用后端中进行一些额外的验证。