1

I got controller which returns list of pathes with images in Json. I want to parse it and output images one by one. But my ajax script not working. (also I added @Html.ActionLink which is working ok when I click on the link).

So heres the code

Script:

<div>

@Html.ActionLink("Header", "GetImagesList") ;



 <script type="text/javascript">

     $.ajax({
        url: "/Header/GetImagesList",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        success: function (text) {
        var  fileList = JSON.parse(text);
            for (var i = 0; i < fileList.length; i++) {
                document.write("<div>" + " <img src="+fileList[i]+" alt=\"image\" /> "+ "</div>");
            }
            alert("test");
        }   
    });
</script>
</div>

Controller

public ActionResult GetImagesList()
{
    var imagesList = Directory.GetFiles("C:\\Images");
    return Json(imagesList, JsonRequestBehavior.AllowGet);
}

3 Answers 3

2

Because the dataType is "json", jQuery should already be parsing the JSON for you and the argument in the success callback should be a JavaScript array. There is no need to parse it again...

success: function (fileList) {
        for (var i = 0; i < fileList.length; i++) {
            document.write("<div>" + " <img src="+fileList[i]+" alt=\"image\" /> "+ "</div>");
        }
    } 
Sign up to request clarification or add additional context in comments.

Comments

0

Try some thing like this...

Result 
{
    for (var i = 0; i < fileList.length; i++) {
        alert("<div>" + " <img src="+fileList[i]+" alt=\"image\" /> "+ "</div>");
    }
} 

public JsonResult GetImagesList()
{
    var imagesList = Directory.GetFiles("C:\\Images");
    return Json(imagesList, JsonRequestBehavior.AllowGet);
}

3 Comments

What changes were made? What about this code solves the problem?
hello.. in response(result I have mentioned) you have to put this code directly...no need of parsing and all..why? itss wrong? and the the controller just change the JsonResult...
The code doesn't make clear that something doesn't need to be parsed. As is, the code is invalid syntax. IF your code is invalid or unclear then that is especially when it needs thorough explaining.
-1

You should try this:

@Html.ActionLink("Header", "GetImagesList",null,new{@id="link"}) ;

 <script type="text/javascript">
$("#link").click(function(){
 $.ajax({
    url: "@Url.Action("GetImagesList","Header"),
    dataType: "json",
    contentType: "application/json; charset=utf-8",
    success: function (text) {
    var  fileList = JSON.parse(text);
        for (var i = 0; i < fileList.length; i++) {
            document.write("<div>" + " <img src="+fileList[i]+" alt=\"image\" /> "+ "</div>");
        }
        alert("test");
    }   
});

});

1 Comment

This is not a good answer. The code is poorly formatted, there appear to be syntax errors, I don't think it solves the problem and you haven't explained what code changes you've made or what this code does differently.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.