SAPUI5简单的ajax操作

瘦欲@ 提交于 2019-11-28 15:23:05
sap.ui.define([
    "sap/ui/thirdparty/jquery",
    "sap/ui/core/mvc/Controller"
], function (jQuery,Controller) {
    "use strict";

    return Controller.extend("sap.ui.demo.walkthrough.controller.App", {

        onGet : function () {

            jQuery.ajax({
                type: "GET",
                contentType: "application/json",
                url: "http://localhost:8080/",
                dataType: "json",
                async: false,
                success: function(data) {
                    alert(data.processInstanceId);
                }
            });
        },


        onPostApply : function () {

            //var dataJson = '{"companyId":"0001","departmentId":"0001"}'

            jQuery.ajax({
                type: "POST",
                url: "http://localhost:8080/apply",
                contentType : "application/json",
                dataType : 'json',
                data: '{"companyId":"0001","departmentId":"0001"}',
                async: false,
                success : function(data) {
                    alert(data.processInstanceId);
                }
            });

        },



        onPostWorkList : function () {

            //var dataJson = '{"companyId":"0001","departmentId":"0001"}'

            jQuery.ajax({
                type: "POST",
                url: "http://localhost:8080/tasklist",
                contentType : "application/json",
                dataType : 'json',
                data: '{"userId":"0001"}',
                async: false,
                success : function(data) {
                    alert(data[0].userFirstName);
                }
            });

        }
    });

});

在sapui5中使用ajax的时候,需要引入 "sap/ui/thirdparty/jquery"。

如果在测试的时候,遇到了下面的错误,No 'Access-Control-Allow-Origin' header is present on the requested resource.

 

那么可以使用以下命令降低Chrome的安全策略来进行测试。

cd C:\Program Files (x86)\Google\Chrome\Application
chrome.exe --disable-web-security --user-data-dir

再执行之前要确保关闭Chrome的全部进程,否则执行不成功。

 

sapui5也提供了别的解决方法,可以查看下面的网址。

Request Fails Due to Same-Origin Policy (Cross-Origin Resource Sharing - CORS)

https://sapui5.hana.ondemand.com/#/topic/672301f4f47640a8b2bc817d2ce0f512

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!