如何从responseText获取特定值

时间:2019-05-22 05:51:06

标签: jquery ajax

提交数据后,我正在使用api,但得到响应,我需要特定值的lead_id

这是成功后我得到的jquery ajax响应:

{readyState: 4, getResponseHeader: ƒ, getAllResponseHeaders: ƒ, 
setRequestHeader: ƒ, overrideMimeType: ƒ, …}
abort: ƒ ( statusText )
always: ƒ ()
complete: ƒ ()
done: ƒ ()
error: ƒ ()
fail: ƒ ()
getAllResponseHeaders: ƒ ()
getResponseHeader: ƒ ( key )
overrideMimeType: ƒ ( type )
pipe: ƒ ( /* fnDone, fnFail, fnProgress */ )
progress: ƒ ()
promise: ƒ ( obj )
readyState: 4
responseText: "done{"lead_id":1511313,"phone":"123456789","message":"Data uploaded successfully"}"
setRequestHeader: ƒ ( name, value )

现在我能够提醒responseText数据,但如何从中获取特定值。

这是我的ajax:

$.ajax({
    method:"GET",
    async: true,
    crossDomain: true,
    url:"check.php?a=api",
    data:datas,
    success:function(res)
    {
        var aa=res.responseText;
    alert(aa);
        console.log(res);
    },
    error:function(res)
    {

        console.log(res);
    }
});

这是我通过它向api发送数据的check.php

header('Content-type: application/json');
if($_GET['a'])
{
    $fname=$_GET['F_Name'];
    $mname=$_GET['M_Name'];
    $lname=$_GET['L_Name'];
    $age=$_GET['Age'];
    $mobile=$_GET['MobileNumber'];
    $email=$_GET['EmailID'];
    $city=$_GET['City'];
    $state=$_GET['State'];

    $url="http://apiurl/api/vendor/upload?cp_id=565&v_id=117&c_id=13&first_name=".$fname."&last_name=".$lname."&email=".$email."&phone=".$mobile;
   }
$handle=fopen($url,"r");
if($handle)
{
    while(!feof($handle))
{
    $buffer=fgets($handle,4096);
    echo $buffer;
}
}
fclose($handle);

我需要lead_id的值

2 个答案:

答案 0 :(得分:2)

请尝试此操作,如果遇到错误,请分享。

var str = responseText.replace('done', '');
str = str.replace('}"', '}');
var data = JSON.parse(str);
console.log(data.lead_id);

答案 1 :(得分:-1)

由此更改responseText数据。

 responseText :{"lead_id":1511313,"phone":"123456789","message":"Data uploaded successfully"};
 console.log(responseText.lead_id);