gpt4 book ai didi

jquery - 使用 Jsonp 调用跨域 Asp.Net Web 服务

转载 作者:行者123 更新时间:2023-12-01 04:58:25 26 4
gpt4 key购买 nike

我尝试了无数的例子,但无法让它发挥作用。

我尝试调用跨域 asp.net Web 服务,但每次都会返回以下错误:

jQuery18105929389187970706_1348249020199 was not called

这是我的网络服务:

[WebService(Namespace = "http://www.mywebsite.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
[ScriptService]
public class DataService : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public string GetIncidentsByAddress()
{
return "It worked!";
}
}

我的 HttpModule 处理 Json:

 public class JsonHttpModule : IHttpModule
{
private const string JSON_CONTENT_TYPE = "application/json; charset=utf-8";

public void Dispose()
{
}

public void Init(HttpApplication app)
{
app.BeginRequest += OnBeginRequest;
app.ReleaseRequestState += OnReleaseRequestState;
}

bool _Apply(HttpRequest request)
{
if (!request.Url.AbsolutePath.Contains(".asmx")) return false;
if ("json" != request.QueryString.Get("format")) return false;
return true;
}

public void OnBeginRequest(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;

if (!_Apply(app.Context.Request)) return;

// correct content type of request
if (string.IsNullOrEmpty(app.Context.Request.ContentType))
{
app.Context.Request.ContentType = JSON_CONTENT_TYPE;
}
}

public void OnReleaseRequestState(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;

if (!_Apply(app.Context.Request)) return;

// apply response filter to conform to JSONP
app.Context.Response.Filter =
new JsonResponseFilter(app.Context.Response.Filter, app.Context);
}
}

public class JsonResponseFilter : Stream
{
private readonly Stream _responseStream;
private HttpContext _context;

public JsonResponseFilter(Stream responseStream, HttpContext context)
{
_responseStream = responseStream;
_context = context;
}

//...

public override void Write(byte[] buffer, int offset, int count)
{
var b1 = Encoding.UTF8.GetBytes(
_context.Request.Params["callback"] + "(");
_responseStream.Write(b1, 0, b1.Length);
_responseStream.Write(buffer, offset, count);
var b2 = Encoding.UTF8.GetBytes(");");
_responseStream.Write(b2, 0, b2.Length);
}

//...
}

我的 HttpModule 的 web.config:

<add name="JSONAsmx" type="JsonHttpModule, App_Code"/>

最后是我的 jQuery 调用:

<script src="js/jquery.jmsajax.min.js" type="text/javascript"></script>
<script src="js/jquery-1.8.1.min.js" type="text/javascript"></script>

<script type="text/javascript">

$.jmsajaxurl = function(options) {
var url = options.url;
url += "/" + options.method;
if (options.data) {
var data = ""; for (var i in options.data) {
if (data != "")
data += "&"; data += i + "=" +
msJSON.stringify(options.data[i]);
}
url += "?" + data; data = null; options.data = "{}";
}
return url;
};

$(function() {
var url = $.jmsajaxurl({
url: "http://www.mywebsite.org/apps/IncidentReportingService/DataService.asmx",
method: "GetIncidentsByAddress",
data: {}
});

$.ajax({
cache: false,
dataType: "jsonp",
success: function(data) { successCallback(data); },
error:function(xhr, status, errorThrown) { debugger;},
url: url + "&format=json"
});

});

function successCallback(data) {
debugger;
$.each(data, function(i, item) {
$("#tweets ul").append("<li>" + item.text + "</li>");
});
};

有什么想法吗?

最佳答案

尝试解析返回的json数据,如下

success: function(data) { 
data = JSON.parse(data);
// Do work with the data
}

关于jquery - 使用 Jsonp 调用跨域 Asp.Net Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12535500/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com