当我试图从http://api-v3.deezer.com/1.0/search/album/?q=beethoven&index=2&nb_items=2&output=json中获得JSON时:
(jQuery 1.6.2)
$.ajax({
type: "GET",
url: url,
dataType: "jsonp",
success: function (result) {
alert("SUCCESS!!!");
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.statusText);
alert(xhr.responseText);
alert(xhr.status);
alert(thrownError);
}
});我明白了:parsererror; 200; undefined; jquery162******************** was not called
但是使用来自http://search.twitter.com/search.json?q=beethoven&callback=?&count=5的JSON可以很好地工作。两者都是有效的JSON格式。那么这个错误是什么呢?
更新
@3 3ngima,我已经在asp.net中实现了它,它工作得很好:
$.ajax({
type: "POST",
url: "WebService.asmx/GetTestData",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
alert(result.d);
}
});WebService.asmx:
[WebMethod]
public string GetTestData()
{
try
{
var req = System.Net.HttpWebRequest.Create("http://api-v3.deezer.com/1.0/search/album/?q=beethoven&index=2&nb_items=2&output=json");
using (var resp = req.GetResponse())
using (var stream = resp.GetResponseStream())
using (var reader = new System.IO.StreamReader(stream))
return reader.ReadToEnd();
}
catch (Exception) { return null; }
}发布于 2011-07-10 21:34:36
这是因为你告诉jQuery你期待的是JSON-P,而不是JSON。但返回是JSON。JSON的名字非常错误,命名的方式让人困惑不已。它是一种通过script标记将数据传递给函数的约定。相反,JSON是一种数据格式。
JSON示例:
{"foo": "bar"}JSON示例:
yourCallback({"foo": "bar"});JSON之所以有效,是因为JSON是JavaScript文字表示法的子集。JSON-P只不过是一个承诺,如果您告诉服务要调用哪个函数名(通常在请求中添加一个callback参数),响应将以functionname(data)的形式出现,其中data将是"JSON“(或者更常见的是,JavaScript文字,这可能不是完全相同的事情)。您应该在script标记的src ( jQuery为您做的)中使用JSON来绕过Same Origin Policy,该Same Origin Policy阻止ajax请求从它们的原始文档来源请求数据(除非服务器支持CORS,浏览器也支持CORS)。
发布于 2011-07-10 21:56:44
如果服务器不支持cross domain请求,则可以:
json,在php中,您可以这样做
proxy.php包含以下代码
<?php
if(isset($_POST['geturl']) and !empty($_POST['geturl'])) {
$data = file_get_contents($_POST['geturl']);
print $data;
}
?>然后向代理发出ajax请求,如下所示
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
$("#btn").click(function(){
alert("abt to do ajax");
$.ajax({
url:'proxy.php',
type:"POST",
data:{geturl:'http://api-v3.deezer.com/1.0/search/album/?q=beethoven&index=2&nb_items=2&output=json'},
success:function(data){
alert("success");
alert(data);
}
});
});
});
</script>试试看我得到了json的回应..。
发布于 2013-02-02 15:58:57
我终于找到了解决办法。首先,webservice或页面中的webmethods对我不起作用,它总是返回xml,在本地工作很好,但是在godaddy这样的服务提供商中却不工作。
我的解决方案是创建一个.ahsx,它是.net中的一个处理程序,并使用传递jsonp的jQuery回调函数包装内容,它可以工作。
[System.Web.Script.Services.ScriptService]
public class HandlerExterno : IHttpHandler
{
string respuesta = string.Empty;
public void ProcessRequest ( HttpContext context )
{
string calls= context.Request.QueryString["callback"].ToString();
respuesta = ObtenerRespuesta();
context.Response.ContentType = "application/json; charset=utf-8";
context.Response.Write( calls +"("+ respuesta +")");
}
public bool IsReusable
{
get
{
return false;
}
}
[System.Web.Services.WebMethod]
private string ObtenerRespuesta ()
{
System.Web.Script.Serialization.JavaScriptSerializer j = new System.Web.Script.Serialization.JavaScriptSerializer();
Employee[] e = new Employee[2];
e[0] = new Employee();
e[0].Name = "Ajay Singh";
e[0].Company = "Birlasoft Ltd.";
e[0].Address = "LosAngeles California";
e[0].Phone = "1204675";
e[0].Country = "US";
e[1] = new Employee();
e[1].Name = "Ajay Singh";
e[1].Company = "Birlasoft Ltd.";
e[1].Address = "D-195 Sector Noida";
e[1].Phone = "1204675";
e[1].Country = "India";
respuesta = j.Serialize(e).ToString();
return respuesta;
}
}//class
public class Employee
{
public string Name
{
get;
set;
}
public string Company
{
get;
set;
}
public string Address
{
get;
set;
}
public string Phone
{
get;
set;
}
public string Country
{
get;
set;
}
}下面是jquery的调用:
$(document).ready(function () {
$.ajax({
// url: "http://www.wookmark.com/api/json",
url: 'http://www.gjgsoftware.com/handlerexterno.ashx',
dataType: "jsonp",
success: function (data) {
alert(data[0].Name);
},
error: function (data, status, errorThrown) {
$('p').html(status + ">> " + errorThrown);
}
});
});而且工作得很完美
加布里埃尔
https://stackoverflow.com/questions/6643838
复制相似问题