Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 如何在HTML单元中获得Ajax响应(它呈现网页的HTML)_Jquery_Ajax_Json_Testing_Htmlunit - Fatal编程技术网

Jquery 如何在HTML单元中获得Ajax响应(它呈现网页的HTML)

Jquery 如何在HTML单元中获得Ajax响应(它呈现网页的HTML),jquery,ajax,json,testing,htmlunit,Jquery,Ajax,Json,Testing,Htmlunit,我有一个web应用程序,当我向服务器发出AJAX请求进行CRUD操作时,它会返回JSON。这是因为我使用jQuery来处理数据,而不刷新页面(MVC),因此如果我在系统中创建一个新条目,服务器将返回一个响应,其中包含JSON格式的创建条目。jQuery管理接收到的数据,并将条目呈现在列表上(使用先前创建的条目) 现在我用HTML单元测试它,但是如果我尝试 WebResponse response = page.getWebResponse() page.asText() 我得到200个状态和

我有一个web应用程序,当我向服务器发出AJAX请求进行CRUD操作时,它会返回JSON。这是因为我使用jQuery来处理数据,而不刷新页面(MVC),因此如果我在系统中创建一个新条目,服务器将返回一个响应,其中包含JSON格式的创建条目。jQuery管理接收到的数据,并将条目呈现在列表上(使用先前创建的条目)

现在我用HTML单元测试它,但是如果我尝试

WebResponse response = page.getWebResponse()
page.asText()
我得到200个状态和信息“OK”。但我期待的是我创建的条目的JSON数据

如果我尝试

WebResponse response = page.getWebResponse()
page.asText()
我得到当前页面的HTML(列表中已经有条目,但没有我想要的数据)

这是一个与此类似的问题,没有回应:

PD:jQuery表单有两个字段,一个是名称的文本输入,另一个是车辆设置的选择。我将要选择的设置列表传递给此函数。该名称由时间戳和静态int种子自动生成。这是我正在使用的代码:

public void create(List<Settings> settings) throws FailingHttpStatusCodeException, MalformedURLException, IOException
{
    page = webClient.getPage("localhost:8080/cars/list");

    int elementsCount = getEntitiesCount();
    creationSeed = elementsCount+1;

    HtmlAnchor anchor = (HtmlAnchor) page.getFirstByXPath("//a[@class='action-createCar']");
    page = (HtmlPage) anchor.click();

    HtmlForm form = page.getFirstByXPath("//form[@class='CarsForm']");
    form = page.getForms().get(0);
    HtmlTextInput nameField = form.getInputByName("CarsForm-name");
    nameField.setText("Test " + creationSeed + " - " + new Date().getTime());

    HtmlSelect columnSelectList = (HtmlSelect) form.getSelectByName("CarsForm-settings");
    if (settings != null && settings.size() > 0)
        for (Settings setting : settings)
        {
            HtmlOption htmlOption = columnSelectList.getOptionByValue(setting.name());
            htmlOption.setSelected(true);
        }

    //looking for the button to submit the form
    HtmlDivision buttonSet = page.getFirstByXPath("//div[@class='ui-dialog-buttonset']");
    HtmlButton okButton = (HtmlButton) buttonSet.getFirstElementChild();

    page = okButton.click();

    assertEquals(elementsCount + 1, getEntitiesCount());

    //Here is where I want to get the server response to check the data which is returned by the server
    //And neither page.getWebResponse() or page.asText() contains it
}
public void create(列表设置)抛出FailingHttpStatusCodeException、MalformedUrlexException、IOException
{
page=webClient.getPage(“localhost:8080/cars/list”);
int elementScont=getEntitiesCount();
creationSeed=ElementScont+1;
HtmlAnchor anchor=(HtmlAnchor)page.getFirstByXPath(“//a[@class='action-createCar']”);
页面=(HtmlPage)锚定。单击();
HtmlForm form=page.getFirstByXPath(“//form[@class='CarsForm']”);
form=page.getForms().get(0);
HtmlTextInput nameField=form.getInputByName(“CarsForm name”);
nameField.setText(“Test”+creationSeed+“-”+newdate().getTime());
HtmlSelect columnSelectList=(HtmlSelect)form.getSelectByName(“CarsForm设置”);
if(settings!=null&&settings.size()>0)
用于(设置:设置)
{
HtmlOption HtmlOption=columnSelectList.getOptionByValue(setting.name());
htmlOption.setSelected(true);
}
//正在查找提交表单的按钮
HtmlDivision buttonSet=page.getFirstByXPath(“//div[@class='ui-dialog-buttonSet']”);
HtmlButton okButton=(HtmlButton)buttonSet.getFirstElementChild();
page=OK按钮。单击();
assertEquals(elementsCount+1,getEntitiesCount());
//这里是我想要获取服务器响应以检查服务器返回的数据的地方
//并且page.getWebResponse()或page.asText()都不包含它
}

}

我遇到了类似的问题,并使用以下方法找到了解决方案:

您可以将HttpWebConnection子类化并覆盖getResponse(),如下所示:

这意味着我们可以包装我们的webClient对象,截取我们需要的任何请求和响应,提取它们或进行更改


我创建了一个扩展WebConnectionWrapper的内部类,其中有一个字段是我需要的JSON:

private class JsonResponseWebWrapper extends WebConnectionWrapper{

    public JsonResponseWebWrapper(WebClient webClient){
        super(webClient);           
    }
    
    String jsonResponse;
    
    @Override
    public WebResponse getResponse(WebRequest request) throws IOException {;
        WebResponse response = super.getResponse(request);
        //extract JSON from response
        jsonResponse = response.getContentAsString();
        return response;
    }

    public String getJsonResponse() {
        return jsonResponse;
    }
};
这样,我们将拦截每一个响应。但首先我们需要包装webClient。我在请求JSON响应之前就这样做了,所以我不必向包装器添加条件:

JsonResponseWebWrapper jrww = new JsonResponseWebWrapper(webClient);
page = button.click();
String rawJSON = jrww.getJsonResponse();
之后,您可以使用您最喜欢的JSON解析器对其进行解析。
希望这有帮助

发布您尝试过的内容“如果我尝试WebResponse=page.getWebResponse(),我会得到200个状态和消息“OK”。但我希望得到我创建的条目的JSON数据。如果我尝试page.asText(),我会得到当前页面的HTML(条目已经在列表中,但不是我想要的数据)请发布您的代码我已经包含了测试用例的代码。显然,如果没有HTML代码,就不可能知道发生了什么。例如:你说你希望
page
得到回复。。。但是,
okButton.click()
do做什么呢?我必须让JSONResponseWebRapper扩展HttpWebConnection,并通过webClient.setWebConnection(JSONResponseWebRapper)使用JSONResponseWebRapper;其中JSONResponseWebRapper=新的JSONResponseWebRapper(网络客户端);