0
votes

I have this situation: in MVC View javascript function I call controller action and get some xml data string and then I call another controller action where I make new object and call new view. I always get error message: Error while request "undefined" : TextStatus : "parsererror" ErrorThrown : "SyntaxError: JSON parse: unexpected character at line 1 column 1 on the JSON data"

I don't have any idea why is this happening. If I direct call second controller action with hardcoded xml inthere I get correct result - new view. How can I call controller action from javascript function in view without refreshing view?

This is my code:

javascript function in view CompleteFrameBrand:

function order(model) {
    $.p({
        url: '@Url.Action("CompleteFrameBrandDetails", "PacCompleteFrame")',
        data: { item: model },
        success: function (result) {
            if (result.Success) {
               $.p({
                    url: '@Url.Action("GlassCompleteFrame", "PacModule")',
                    data: JSON.stringify({ b2bXml: result.Data }),
                    success: function (result) {
                        return;
                    }
                });

            } else {
                $.alert({
                    message: 'error while trying to load xml details'
                });
            }
        }
    });

CompleteFrameController::CompleteFrameBrandDetails(string item)

     public ActionResult CompleteFrameBrandDetails(string item)
    {
        var customerNumber = _workContext.SelectedCustomerNumber;
        var employeeId = _workContext.CurrentCustomer.Ll3Id;
        string b2bxml = _completeFrameService.GetCFB2BXML(employeeId, customerNumber, item, _workContext.WorkingLanguage.LanguageCulture.Substring(3));
        return new JsonResult()
        {
            Data = new
            {
                Success = true,
                Data = b2bxml
            }
        };
    }

PacModuleController::GlassCompleteFrame:

    public ActionResult GlassCompleteFrame(string b2bXml)
    {
        PacModuleModel model = new PacModuleModel();
        model.CustomerNumber = _workContext.SelectedCustomerNumber;
        model.Language = _workContext.WorkingLanguage.UniqueSeoCode;
        model.Comission = "";
        model.GlassXml = b2bXml.Replace("\"", "\\\"");
        model.Price = Convert.ToDouble(p, System.Globalization.CultureInfo.InvariantCulture);
        model.ReadOnly = false;

        return View("Glass", model);
    }
1
Are you sure that first request is XML? It looks more like JSON. Where exactly does the error get thrown? When constructing the data for the second request? When sending the second request? When receiving the response from the second request? What is the response? It looks like a view, what do you plan to do with the response? Currently your success handler does nothing.David
In first request I get string and that string I forward as parameter in second request but in this case I don't want any response because I want to show other view. Error get thrown at second request, I think by response but I don't know how to call controller action without ajax.Korl
In your second request, you are returning a ViewResult, not a JSONResult like your first request. What is this Glass view returning? I'm guessing it isn't JSON or XML.Mister Epic
You are right. In Glass view I don't returning anything. I didn't think that I can call View("Glass", model); stand alone and return some dummy JSONResult. I will try that. Thank you for hint.Korl
hmmm this don't work. What I need is call Controller Action from javascript function without using Ajax. How can I do that? window.location = url goes directly to view but I need to call controller action.Korl

1 Answers

0
votes

I'm assuming that $.p is a wrapper for jQuery $.ajax in which case setting the dataType to xml should help. Without explicitely setting dataType jQuery will make a best guess and it seems to think you are returning json.

It's also possible you have set defaults somewhere for json.