为了账号安全,请及时绑定邮箱和手机立即绑定

Vue如何调用webservice

标签:
C#

首先在asp.net项目中的web.config中加入:

 <system.webServer>
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Methods" value="OPTIONS,POST,GET"/>
        <add name="Access-Control-Allow-Headers" value="x-requested-with,content-type,Accept"/>
        <add name="Access-Control-Allow-Origin" value="*" />
      </customHeaders>
    </httpProtocol>
  </system.webServer>

然后在AppCode中的WebService.cs中加入:

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。 
// [System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService
{
    [WebMethod(Description = "GetUserinfoList")]
    public string GetUserinfoList()
    {
        DataSet ds = null;
        string json = "";
        string sql = "";
        try
        {
            SqlParameter[] paras = new SqlParameter[1];
            paras[0] = new SqlParameter("@today", SqlDbType.VarChar);
            paras[0].Value = "";
            sql = @"select * from Userinfo";
            ds = SqlHelper.ExecuteDataset(getLocalConnection(), CommandType.Text, sql, paras);
            if (ds == null || ds.Tables[0].Rows.Count == 0)
                json = "";
            else
                json = JsonConvert.SerializeObject(ds.Tables[0], new DataTableConverter());
        }
        catch (Exception ex)
        {
            json = "";
        }
        return json;
    }

然后在vue的项目中引入js:

npm install jquery --save

然后在vue.config.js加入:

const webpack = require('webpack')
module.exports = {
  devServer: {
    overlay: {
      warnings: false,
      errors: false
    }
  },
  lintOnSave: false,
  publicPath: './',
  chainWebpack: config => {
    config.plugin('provide').use(webpack.ProvidePlugin, [{
      $: 'jquery',
      jquery: 'jquery',
      jQuery: 'jquery',
      'window.jQuery': 'jquery'
    }])
  }
}

然后在页面中这么来使用:

<template>
    <el-table
            :data="userData"
            border
            style="width: 100%">
        <el-table-column
                prop="UserId"
                label="Id"
                width="180">
        </el-table-column>
        <el-table-column
                prop="UserName"
                label="姓名"
                width="180">
        </el-table-column>
        <el-table-column
                prop="Address"
                label="地址">
        </el-table-column>
    </el-table>
</template>

<script>
    import {tqData,getUser} from '@/network/networks'
    import $ from 'jquery'
    import axios from "axios";
    export default {

        mounted() {
            var _this = this;
            $.ajax({
                    type: "post",
                    url: "http://localhost:57651/WebService.asmx/GetUserinfoList",    //接口地址,后面加上 /方法名即可
                    contentType: "application/json",
                    dataType: "json",
                    success: function (inf) {

                        _this.$nextTick(() => {
                            _this.userData = JSON.parse(inf.d);    //将获取的JSON字符串转化成JSON对象
                            console.log("<<<111");
                            console.log(this.userData);
                            _this.itemkey = Math.random();
                        });
                    },
                    error: function () {
                        alert("获取数据失败!");
                    }
            });
        },
        methods:{
        },
        data() {
            return {
                itemkey:'',
                userData: []
            }
        }
    }
</script>


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
全栈工程师
手记
粉丝
6508
获赞与收藏
303

关注作者,订阅最新文章

阅读免费教程

  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消