我正在尝试使用下面的JavaScript从另一个域调用我的Web服务(WCF Restful)(在以下代码中,它们都在localhost中但具有不同的端口号,我认为它们被视为跨域请求)。 / p>
jQuery.support.cors = true;
$(document).ready(function () {
{
$.ajax({
type: "POST",
url: "http://localhost:52032/Service1.svc/std",
contentType: "application/jsonp; charset=utf-8",
dataType: "jsonp",
processData: false , //Same result if i remove this line
crossDomain : true , //Same result if i remove this line
converters: window.String, //Same result if i remove this line
success: OnSuccess,
error: OnError});
}
});
function OnSuccess(data)
{
alert("Finaly :D");
JSON.stringify(data);
}
function OnError(request, status, error)
{
alert("Error: "+request.statusText + JSON.stringify(request)+ " | " +JSON.stringify(status) +" | " +JSON.stringify(error));
}
我的网络服务非常简单。 getStudents()
返回JSON字符串。我使用JavaScriptSerializer
将我的学生对象解析为JSON。
namespace MyFirstWcfTestService
{
[ServiceContract]
public interface IService1
{
[OperationContract(Name = "AddParameter")]
[WebGet(UriTemplate = "/std", ResponseFormat = WebMessageFormat.Json)]
String getStudents();
}
}
我总是从OnError
函数中收到以下错误,当我在Chrome开发/调试模式中查看响应时,我可以看到我的JSON文本。
错误: 成功{ “readyState的”:4, “状态”:200, “状态文本”: “成功”} | “ParserError” | “jQuery1706816276672761887_1322704382563未被称为”
我返回的json文本如下所示:
“[{\” StudentID \ “:256,\” StudentFirstName \ “:\” 阿里\ “\ ”StudentSurname \“:\ ”史密斯\“},{\ ”StudentID \“:306,\” StudentFirstName \ “:\” 约翰\”,\ “StudentSurname \”:\ “MENZ \”},{\ “StudentID \”:314,\ “StudentFirstName \”:\ “乔治·\”,\ “StudentSurname \”: \ “雷\”},{\ “StudentID \”:316,\ “StudentFirstName \”:\ “佛瑞德\”,\ “StudentSurname \”:\ “帕特里克\”},{\ “StudentID \”:603, \ “StudentFirstName \”:\ “乔治\”,\ “StudentSurname \”:\ “福斯特\”},{\ “StudentID \”:604,\ “StudentFirstName \”:\ “伯比\”,\“StudentSurname \ “:\” KOLMAN \ “},{\” StudentID \ “765 \” StudentFirstName \ “:\” 吉姆\”,\ “StudentSurname \”:\ “哈斯\”},{\ “StudentID \”: 987,\ “StudentFirstName \”:\ “哈利\”,\ “StudentSurname \”:\ “波特\”},{\ “StudentID \”:988,\ “StudentFirstName \”:\ “精读\”,\” StudentSurname \ “:\” Mench \ “},{\” StudentID \ “:1001,\” StudentFirstName \ “:\” 吉姆\ “\ ”StudentSurname \“:\ ”科隆\“}]”
请您帮我找到解决此问题的方法。我已经将我的Web服务从SOAP改为这个宁静的了。
修改 我设法将一个jsonp消息包含在一个方法中(myCallback如下)
myCallBack函数([{ “StudentID”:256, “StudentFirstName”: “拉结”, “StudentSurname”: “史密斯”},{ “StudentID”:306, “StudentFirstName”: “阿里”, “StudentSurname”:”弗莱明 “},{” StudentID “:314,” StudentFirstName “:” 乔治”, “StudentSurname”: “雷人”},{ “StudentID”:316, “StudentFirstName”: “弗雷德”, “StudentSurname”: “帕特里克” },{ “StudentID”:603, “StudentFirstName”: “乔治”, “StudentSurname”: “福斯特”},{ “StudentID”:604, “StudentFirstName”: “伯比”, “StudentSurname”: “KOLMAN”}, { “StudentID”:765, “StudentFirstName”: “吉姆”, “StudentSurname”: “哈斯”},{ “StudentID”:987, “StudentFirstName”: “哈利”, “StudentSurname”: “波特”},{” StudentID “:988,” StudentFirstName “:” CON”, “StudentSurname”: “Mench”},{ “StudentID”:1001, “StudentFirstName”: “吉姆”, “StudentSurname”: “冒号”}]);
我还改变了我的Ajax调用,如下所示
$.ajax({
type: "post",
url: "http://localhost:52032/Service1.svc/getStd?callback=myCallback", //or callback=?
dataType: "jsonp",
//converters: jQuery.parseJSON,
contentType: 'application/javascript',
success: OnSuccess,
error: OnError
});
但我仍然得到同样的错误。我为这么简单的任务徘徊了3个星期:(
答案 0 :(得分:0)
尝试更改getStudents()以返回Json(List,JsonRequestBehavior.AllowGet)并通过jQuery访问返回的列表,每个使用数据[iterator] .StudentID,data [iterator] .StudentFirstName等。
答案 1 :(得分:0)
尝试此更改:
namespace MyFirstWcfTestService
{
[ServiceContract]
public interface IService1
{
[OperationContract(Name = "AddParameter")]
[WebGet(UriTemplate = "/std", ResponseFormat = WebMessageFormat.Json)]
List<StudentDTO>getStudents();
}
class StudentDTO
{
public int StudentID;
public string StudentFirstName;
public string StudentSurname;
}
}
在客户端:
function OnSuccess(data)
{
for(var i=0;i<data.length;i++)
{
var fn = data[i].StudentFirstName;
var sn = data[i].StudentSurName;
...
}
}