zoukankan      html  css  js  c++  java
  • ajax 跨域调用webservice 使用jsonp解决方法

    前端 :

    $.ajax({
    type: "POST", //访问WebService使用Post方式请求
    contentType: "application/json;charset=utf-8", //WebService 会返回Json类型
    url: "http://localhost:42072/WebService/Service.asmx/HelloWorld?jsonpcallback=?", //调用WebService
    data: "{}",
    dataType: 'jsonp',
    success: function(response) { //回调函数,result,返回值

    //var json = eval('(' + response+ ')');
    alert(response.name);

    }
    });

    后端:

    [WebMethod]
    public void HelloWorld()
    {
    Context.Response.ClearHeaders();

    Context.Response.Charset = "UTF-8";

    Context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
    JavaScriptSerializer js = new JavaScriptSerializer();
    string jsonpCallback = HttpContext.Current.Request.QueryString["jsonpcallback"];
    person p = new person();
    p.name = "Hello World";
    string jsonStr = js.Serialize(p);

    jsonStr= jsonpCallback + "(" + jsonStr + ")";

    Context.Response.Write(jsonStr);

    Context.Response.End();

    }
    class person
    {
    public string name;
    }

  • 相关阅读:
    dup和dup2
    cassandra nodetools
    python 之mechanize
    IDEA使用GsonFormat
    游标应用
    SQL 2005 with(nolock)详解
    SET NOCOUNT ON
    异常处理机制(Begin try Begin Catch)
    FILLFACTOR 作用 sql
    触发器语法
  • 原文地址:https://www.cnblogs.com/wdnrsjd/p/8820168.html
Copyright © 2011-2022 走看看