簡體   English   中英

Ajax到Web服務的Asmx文件返回Xml值,但需要json值

[英]Ajax to Webservice Asmx file returns Xml value but need json value

單擊按鈕時,asp.net頁面中的簡單HTML按鈕Ajax代碼將調用傳遞給服務頁面方法並返回值,但XML格式需要在json中使用它

Ajax代碼

$(document).ready(function(){
    $("#btnSubmit").click(function(){
        txtNameValue = $("#txtName").val();
       alert("button clicked");

        $.ajax({
        type: "POST",
        url: serviceURL+"/HelloWorld",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: true,
        success:function(response) {

            console.log(response);
        }
        });

    });    
});

服務頁面代碼

using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;



/// <summary>
/// Summary description for FirstPageWebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//[ScriptService]
public class FirstPageWebService : System.Web.Services.WebService {

    public FirstPageWebService () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }


    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string HelloWorld() {

        return "Hello World";
    }

}

在HTML網頁中通過網絡跟蹤時在XML中獲得此響應


<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">Hello World</string>

錯誤圖片請點擊這里

您可以在服務方法中使用JavaScriptSerializer

return new System.Web.Script.Serialization.JavaScriptSerializer().Serialize("Hello World");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM