이 질문은 여러 번 답변되었지만 작동하지는 못합니다. 제 질문은 one과 같으며이 one과 third example입니다.JSON 객체 배열을 반복하여 반복합니다.
내가하고 싶은 것은 this 및 this 개의 질문과 같은 JSON 객체의 옵션 상자를 채우는 것입니다. 그들은 모두 약간 다르지만 유사하지만 작동하도록 할 수는 없습니다. ;
function Test() {
$.ajax({
type: "POST",
url: "Service.asmx/HelloWorld",
data: "{'p_productCategoryId' : 1 }",
contentType: "application/json; charset=utf-8",
dataType: "json",
success:function(msg){
alert(msg.d)
},
error: function() {
alert("An error has occurred during processing your request.");
}
});
} :
<System.Web.Script.Services.ScriptService()> _
<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class Service
Inherits System.Web.Services.WebService
<WebMethod()> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
Public Function HelloWorld(ByVal p_productCategoryId As Integer) As String
Dim productCategory = ProductService.GetProductCategory(p_productCategoryId)
'Dim productList = ProductService.GetProducts(productCategory)
Dim productList = New List(Of Product)
For cnt = 1 To 3
Dim product = New Product(cnt)
product.Productname = cnt.ToString & "|" & cnt.ToString
productList.Add(product)
Next
Return productList.ToJSON
End Function
End Class
<System.Runtime.CompilerServices.Extension()> _
Public Function ToJSON(Of T)(p_items As List(Of T)) As String
Dim jSearializer As System.Web.Script.Serialization.JavaScriptSerializer = New System.Web.Script.Serialization.JavaScriptSerializer()
Return jSearializer.Serialize(p_items)
End Function
나는 다음과 같은 코드를 사용하는 경우 다음은 웹 서비스에서 내 코드는 OK 보인다
[{"Id":1,"IsActive":false,"Category":null,"Productname":"1|1","Price":0},
{"Id":2,"IsActive":false,"Category":null,"Productname":"2|2","Price":0},
{"Id":3,"IsActive":false,"Category":null,"Productname":"3|3","Price":0}]
:
나는이 결과를 얻는다.
msg에서 'd'를 삭제하면 경고의 결과는 다음과 같습니다
[object Object]
옵션 상자를 채우기 위해 '업무 진행'코드가 순간 :)에 (이것이다 :
function Test() {
$.ajax({
type: "POST",
url: "Service.asmx/HelloWorld",
data: "{'p_productCategoryId' : 1 }",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
$("#prd_id").empty().append($("<option></option>").val("[-]").html("Please select"));
$.each(msg.d, function() {
$("#prd_id").append($("<option></option>").val(this['Id']).html(this['Productname']));
});
},
error: function() {
alert("An error has occurred during processing your request.");
}
});
};
나는 이전에 언급 한 예제에서 작동하도록 여러 가지 방법을 시도했지만 아무 소용이 없습니다. msg.d를 사용하면 문자열의 문자 수와 함께 옵션 상자가 채워집니다. 내가 명시 적으로 'getJSON'와 'msg'에서 JSON 개체를 만들려고했습니다. ('데이터 유형'과 같은 것이 아닌가요?) 예제 데이터에서 볼 수 있듯이 명명 된 객체를 사용할 수 없기 때문에 사용할 수 없습니다. 내가 뭘 놓치고 있니?
배열을보기 위해 코드를 가져올 수없는 방법에는 3 가지 항목이 있습니다.
위 TJ 크라우에 대한 내 대답을 참조하십시오 @Palash. 어떤 웹 서비스에서 내 메시지에 존재하지 않습니다. 그것이 작동하지 않는 이유를 설명합니다. 귀하의 예제에서 어떤 식 으로든 당신은 'd'를 사용하므로 작동하지 않습니다. 정말로 멋진 예제를 만들었습니다. – Sigur
@Sigur : msg.d를주의 깊게 말하면서 msg.d를 배열이라고 생각했습니다. (ASP.Net이하는 일입니다. 왜 그런지 이해하지 못했습니다). 'msg'가 배열이면'.d' 만 제거하면됩니다. –
@Sigur : 위의 두 가지 추가 노트를 추가했습니다. –