简体   繁体   English

jQuery Ajax调用不返回任何内容

[英]jQuery Ajax call not returning anything

I have an Asynchronous call in jQuery where a POST request returns an HTTP 200, but there is no response text or anything to work with from the endpoint in question. 我在jQuery中有一个异步调用,其中POST请求返回一个HTTP 200,但是没有响应文本或任何可从该端点处理的内容。

I'm confused as to what could be the cause on my Localhost, as when I use the same call to poll a service like JSONTest, I get a valid object back in return. 我对Localhost的原因感到困惑,因为当我使用相同的调用来轮询JSONTest之类的服务时,我会得到一个有效的对象作为回报。

This is what the result endpoint looks like, written in PHP using Slim 这就是使用Slim用PHP编写的结果端点的样子

$app->post("/search", function() use ($app) {
    try {
        $request = $app->request;
        $body = $request->getBody();
        $input = json_decode($body);

        //Prepare search string
        $query = "%". $input->query . "%";
        $grade = '%grade ' . $input->grade . "%";
        $meta = $input->meta;

        $proc_results = array();

        $item = new stdClass();
        $item->id = 1;
        $item->source = "source";
        $item->type = "lesson_plan";
        $item->description = "Description of the Lesson Plan";
        $item->date_created = 1234567890;

        $proc_results[] = $item;

        $app->response()->header('Content-Type','application/json');
        $app->response()->body(json_encode($proc_results));

        } catch (Exception $e) {

        }
    });

This call does return a JSON response when using a utility like POSTMAN, but when I use the following test jQuery code, I get an Object that has no responseText or any sign that my interpreter has the object. 使用POSTMAN之类的实用程序时,此调用的确返回JSON响应,但是当我使用以下测试jQuery代码时,得到的对象没有任何responseText或任何解释器具有该对象的符号。

$.ajax({
    "type":"POST",
    "url":"http://localhost:9001/search",
    "data":{"query":"math","grade":"4"}
}).done(function(result) { 
    console.debug(result);
});

Am I missing a component in my done() call to poll the resources? 我是否在我的done()调用中缺少用于轮询资源的组件? Is my Slim call sending malformed JSON? 我的Slim通话是否发送格式错误的JSON? If needed I can get a working demo up online. 如果需要,我可以在线获取有效的演示。

Type, url, data should not be strings. 类型,网址,数据不应为字符串。 Try as not string. 尝试不使用字符串。 It should work. 它应该工作。 Also the keys of data shouldnot be string. 同样,数据键也不应该是字符串。

Try this 尝试这个

$.ajax({
    type:"POST",
    url:"/search",
    data:{query:"math",grade:"4"}
}).done(function(result) { 
    console.debug(result);
});

Try setting the datatype to be JSON, expanding on @doniyor's answer: 尝试将数据类型设置为JSON,扩展@doniyor的答案:

$.ajax({
    type:"POST",
    url:"/search",
    datatype:"json",
    data:{query:"math",grade:"4"}
}).done(function(result) { 
    console.debug(result);
})

see: http://api.jquery.com/jquery.ajax/ 参见: http : //api.jquery.com/jquery.ajax/

Seems from your comments that you are looking for JSON as a result. 从您的评论看来,您正在寻找JSON结果。

I found the root cause: I was not sending valid JSON for PHP to parse. 我找到了根本原因:我没有为PHP解析发送有效的JSON。 By adding JSON.stringify it responds as expected: 通过添加JSON.stringify,它可以按预期响应:

$.ajax({
    type:"POST",
    url:"http://localhost:9001/search",
    dataType:"json",
    contentType: "application/json",
    data: JSON.stringify({"query": "math", "grade": "4", "meta": "z"}) 
}).done(function(result) { 
    console.debug(result);
});

Thanks guys for helping. 谢谢你们的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM