0
私のアプリケーションはMVC 5 C#です。私は、このAjaxのスクリプトを使用してコントローラから文字列値を取得しようとしています :ここJsonは別の文字列を返す
function testing() {
$.ajax({
url: "../../Home/Test",
type: "GET",
data: {
id: 1
},
success: function (result) {
if (result.PhotoURL) {
$("#Photo").html(result.PhotoURL);
}
},
error: function (result) {
alert("error");
}
});
}
がコントローラである:
public ActionResult Test(int id)
{
int updateid = 1;
string thephoto = "<img src=\"@Url.Action(\"TheImage\",
new {id = " + updateid +
"})\" class=\"mg-responsive\" style=\"margin-left:
auto;margin-right: auto\"/> ";
var result = new
{
PhotoURL = thephoto
};
return Json(result, JsonRequestBehavior.AllowGet);
}
私が取得:
<img class="mg-responsive" style="margin-right: auto; margin-left: auto;"
src="@Url.Action(" {id='1})"' new="" thespillimage",="">
あなたは何を得ることを望んでいましたか? – ADyson
– hncl
これはあなたの 'Test()'メソッドが返すものです。 'string'に' @ Url.Action'のようなものを使用している点はありません。これはサーバーで評価され、スクリプトで評価されないカミソリコードです。実際のURLを返すだけです(メソッド '' url = Url.Action(...); 'を使用してURLを生成してください) –