您的位置:首页 > 其它

ajax调用geoServer rest接口实现数据发布

2018-03-21 16:15 429 查看
geoserver服务端配置见前述文章,这里把我实现的几段js代码记录备忘。
api文档见http://docs.geoserver.org/2.12.2/user/rest/index.html#rest
为方便测试,提前把认证配置文件rest.properties改了一下/**;POST,GET,PUT=IS_AUTHENTICATED_ANONYMOUSLY
/**;DELETE=ROLE_ADMINISTRATOR数据目录形式:工作区>数据存储>图层
创建工作区及发布服务时,返回结果会报类似于“parsererrorSyntaxError: Unexpected end of JSON input”这样的错误,还不清楚什么原因,但是不影响创建结果。
1、得到工作区
  //get
  $.ajax({
  url: 'http://localhost:8080/geoserver/rest/workspaces',
  dataType: 'json',
  type: 'GET',
  data:null,
  success: function(data, textStatus){
          //data可能是xmlDoc、jsonObj、html、text等等
         console.log(data);
    this;  //调用本次ajax请求时传递的options参数
       },
    error:function(XMLHttpRequest, textStatus, errorThrown){
          //通常情况下textStatus和errorThrown只有其中一个包含信息
        console.log(textStatus+errorThrown);  
    this;   //调用本次ajax请求时传递的options参数
      }
  })
2、新建工作区
  $.ajax({
    url: 'http://localhost:8080/geoserver/rest/workspaces',
    dataType: 'json',
    contentType: 'application/json',
    type: 'POST',
//     async: false,
//     processData:false,
    data: JSON.stringify({"workspace":{"name":"test03"}}),
    success: function(data, textStatus){
            //data可能是xmlDoc、jsonObj、html、text等等
           console.log(data);
    this;  //调用本次ajax请求时传递的options参数
         },
    error:function(XMLHttpRequest, textStatus, errorThrown){
          //通常情况下textStatus和errorThrown只有其中一个包含信息
        console.log(textStatus+errorThrown);  
    this;   //调用本次ajax请求时传递的options参数
        },
        headers: {
          'Authorization': 'Basic YWRtaW46Z2Vvc2VydmVy'
    }
  })
3、得到数据存储
   $.ajax({
  url: 'http://localhost:8080/geoserver/rest/workspaces/nyc/datastores',
  dataType: 'json',
  type: 'GET',
  data:null,
  success: function(data, textStatus){
      //data可能是xmlDoc、jsonObj、html、text等等
     console.log(data);
    this;  //调用本次ajax请求时传递的options参数
   },
    error:function(XMLHttpRequest, textStatus, errorThrown){
          //通常情况下textStatus和errorThrown只有其中一个包含信息
        console.log(textStatus+errorThrown);  
    this;   //调用本次ajax请求时传递的options参数
  }
})

4,上传发布压缩后的shp文件,如果需要可以创建数据存储

/*var formTest=document.getElementById("form1");
console.log(formTest);*/
var file=$('#uploadFile')[0].files[0];
var formData = new window.FormData();
        formData.append('file', file);
$.ajax({
  url: 'http://localhost:8080/geoserver/rest/workspaces/test01/datastores/test/file.shp',
  dataType: 'json',
  contentType:'application/zip',
  processData: false,
  type: 'PUT',
  async: false,
  data:formData,
  success: function(data, textStatus){
      //data可能是xmlDoc、jsonObj、html、text等等
     console.log(data);
    this;  //调用本次ajax请求时传递的options参数
   },
    error:function(XMLHttpRequest, textStatus, errorThrown){
          //通常情况下textStatus和errorThrown只有其中一个包含信息
        console.log(textStatus+errorThrown);  
    this;   //调用本次ajax请求时传递的options参数
  }
})
参考文档http://blog.csdn.net/dahongdahong/article/details/54629407
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  geoserver rest ajax 服务