前端代碼
$.ajax({ type: "POST", url: "http://localhost:9767/WebService1.asmx/HelloWorld?jsoncallback=?", data: "{}", dataType: "jsonp", success: function (data) { alert(data.result); } });
webservice 的代碼
public void HelloWorld() { string callbackMethodName = HttpContext.Current.Request.Params["jsoncallback"] ?? ""; HttpContext.Current.Response.Write(callbackMethodName + "({result:'Hello World'})"); HttpContext.Current.Response.End(); }
這樣會得到
但是如果用這樣的代碼
如果url不加?jsoncallback=?
$(function () { $.ajax({ type: "POST", url: "http://localhost:9767/WebService1.asmx/HelloWorld", data: "{}", dataType: "jsonp", success: function (data) { alert(data.result); } });
public string HelloWorld() { return "Hello World"; }
則會返回一個xml (
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">Hello World</string>
) 并且會不去回調函數 這是怎么回事,筆者對webservice 不是很了解 也是剛剛接觸 有知道的能告訴下筆者 筆者會萬分感謝
新聞熱點
疑難解答