我试图使用下面的JavaScript从另一个域(在以下代码中,它们都在localhost中,但具有不同的端口号,我认为这是跨域请求)调用我的Web服务(WCF Restful)。
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,“ statusText”:“成功”} |“ ParserError” |
“未调用jQuery1706816276676672761887_1322704382563”
我返回的json文本如下所示:
“ [{\” StudentID \“:256,\” StudentFirstName \“:\” Ali \“,\” StudentSurname \“:\” Smith \“},{\” StudentID \“:306,\” StudentFirstName \“ :\“ John \”,\“ StudentSurname \”:\“ Menz \”},{\“ StudentID \”:314,\“ StudentFirstName \”:\“ George \”,\“ StudentSurname \”:\“ Ray \“},{\” StudentID \“:316,\” StudentFirstName \“:\” Fred \“,\” StudentSurname \“:\” Patric \“},{\” StudentID \“:603,\” StudentFirstName \“:\” George \“,\” StudentSurname \“:\” Foster \“},{\” StudentID \“:604,\” StudentFirstName \“:\” Bobbie \“,\” StudentSurname \“:\ “ Kolman \”},{\“ StudentID \”:765,\“ StudentFirstName \”:\“ Jim \”,\“ StudentSurname \”:\“ Khas \”},{\“ StudentID \”:987,\ “ StudentFirstName \”:\“ Harry \”,\“ StudentSurname \”:\“ Potter \”},{\“ StudentID \”:988,\“ StudentFirstName \”:\“ Con \”,\“ StudentSurname \” :\“ Mench \”},{\“ StudentID \”:1001,\“ StudentFirstName \”:\“ Jim \”,\“ StudentSurname \”:\“ Colon \”}]“
您能帮我找到解决此问题的方法吗?我已经将我的Web服务从SOAP更改为这种宁静的服务。
编辑
我设法将jsonp消息包装在一个方法中(如下所示的myCallback
myCallback([{“ StudentID”:256,“ StudentFirstName”:“ Rachel”,“ StudentSurname”:“ Smith”},{“ StudentID”:306,“ StudentFirstName”:“ Ali”,“ StudentSurname”:“ Flemming”} ,{“ StudentID”:314,“ StudentFirstName”:“ George”,“ StudentSurname”:“ Ray”},{“ StudentID”:316,“ StudentFirstName”:“ Fred”,“ StudentSurname”:“ Patric”},{ “ StudentID”:603,“ StudentFirstName”:“ George”,“ StudentSurname”:“ Foster”},{“ StudentID”:604,“ StudentFirstName”:“ Bobbie”,“ StudentSurname”:“ Kolman”},{“ StudentID “:765,” StudentFirstName“:” Jim“,” StudentSurname“:” Khas“},{” StudentID“:987,” StudentFirstName“:” Harry“,” StudentSurname“:” Poter“},{” StudentID“: 988,“ StudentFirstName”:“ Con”,“ StudentSurname”:“ Mench”},{“ StudentID”:1001,“ StudentFirstName”:“ Jim”,“ StudentSurname”:“ colon”}]));
而且我还按照以下方式更改了我的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
});
但是我仍然遇到同样的错误。我为这样一个简单的任务而徘徊了三个星期:(
最佳答案
尝试更改getStudents()以返回Json(List,JsonRequestBehavior.AllowGet)并通过jQuery通过返回的列表分别使用data [iterator] .StudentID,data [iterator] .StudentFirstName等访问返回列表。